ossa_typeable/
internal.rs

1use crate::Typeable;
2/// Internal helper functions. Do not rely on this API. It is unstable.
3pub use sha2::{Digest, Sha256};
4
5pub fn helper_counter(h: &mut Sha256, n: usize) {
6    let c: u8 = n.try_into().expect(&format!("Too many inputs: {}", n));
7    h.update([c]);
8}
9
10pub fn helper_string(h: &mut Sha256, s: &'static str) {
11    // Check if there are invalid characters
12    let is_valid_string = s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
13    assert!(is_valid_string, "Invalid string: {}", s);
14
15    helper_string_non_ascii(h, s);
16}
17
18pub fn helper_string_non_ascii(h: &mut Sha256, s: &'static str) {
19    helper_counter(h, s.len());
20    h.update(s);
21}
22
23pub fn helper_u8(h: &mut Sha256, n: u8) {
24    h.update([n]);
25}
26
27pub fn helper_usize(h: &mut Sha256, n: usize) {
28    h.update(n.to_be_bytes());
29}
30
31pub fn helper_type_constructor(h: &mut Sha256, constructor: &'static str) {
32    helper_string(h, constructor);
33}
34
35pub fn helper_type_args_count(h: &mut Sha256, type_args_count: u8) {
36    h.update([type_args_count]);
37}
38
39pub fn helper_type_ident<T: Typeable>(h: &mut Sha256) {
40    h.update(<T as Typeable>::type_ident().identifier());
41}