In C, it's easy for us to look up a hash algorithm structure by its
offset by simply indexing the hash_algos array. However, in Rust, we
sometimes need a pointer to pass to a C function, but we have our own
hash algorithm abstraction.
To get one from the other, let's provide a simple function that looks up
the C structure from the offset and expose it in Rust.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
return oid_to_hex_r(buf, algop->empty_tree);
}
+const struct git_hash_algo *hash_algo_ptr_by_number(uint32_t algo)
+{
+ if (algo >= GIT_HASH_NALGOS)
+ return NULL;
+ return &hash_algos[algo];
+}
+
uint32_t hash_algo_by_name(const char *name)
{
if (!name)
ctx->algop->final_oid_fn(oid, ctx);
}
+const struct git_hash_algo *hash_algo_ptr_by_number(uint32_t algo);
/*
* Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
* the name doesn't match a known algorithm.
use std::error::Error;
use std::fmt::{self, Debug, Display};
+use std::os::raw::c_void;
pub const GIT_MAX_RAWSZ: usize = 32;
HashAlgorithm::SHA256 => &Self::SHA256_NULL_OID,
}
}
+
+ /// A pointer to the C `struct git_hash_algo` for interoperability with C.
+ pub fn hash_algo_ptr(self) -> *const c_void {
+ unsafe { c::hash_algo_ptr_by_number(self as u32) }
+ }
+}
+
+pub mod c {
+ use std::os::raw::c_void;
+
+ extern "C" {
+ pub fn hash_algo_ptr_by_number(n: u32) -> *const c_void;
+ }
}