use std::ffi::{CString, NulError};
use rebound_bind as rb;
pub fn hash(str_: &str) -> u32 {
let head = str_.split('\0').next().unwrap_or_default();
let cstr = unsafe { CString::from_vec_unchecked(head.as_bytes().to_vec()) };
unsafe { rb::reb_hash(cstr.as_ptr()) }
}
pub fn try_hash(str_: &str) -> Result<u32, NulError> {
let cstr = CString::new(str_)?;
unsafe { Ok(rb::reb_hash(cstr.as_ptr())) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash_with_nul() {
assert_eq!(hash("hello\0world"), hash("hello"));
assert_eq!(hash("hello"), hash("hello"));
assert!(try_hash("hello\0world").is_err());
assert!(try_hash("hello, world").is_ok());
}
}