MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / encode

Function encode

atomic-core/src/pristine/git_closure.rs:24–48  ·  view source on GitHub ↗
(
    closure: &[Hash],
    mut intern: impl FnMut(&[u8; 32], &[u8; 64]) -> PristineResult<()>,
)

Source from the content-addressed store, hash-verified

22}
23
24pub(crate) fn encode(
25 closure: &[Hash],
26 mut intern: impl FnMut(&[u8; 32], &[u8; 64]) -> PristineResult<()>,
27) -> PristineResult<Vec<u8>> {
28 let count =
29 u32::try_from(closure.len()).map_err(|_| invalid("Git closure exceeds u32 length"))?;
30 if !closure
31 .windows(2)
32 .all(|pair| pair[0].as_bytes() < pair[1].as_bytes())
33 {
34 let mut bytes = Vec::with_capacity(4 + closure.len() * 32);
35 bytes.extend_from_slice(&count.to_le_bytes());
36 for hash in closure {
37 bytes.extend_from_slice(hash.as_bytes());
38 }
39 return Ok(bytes);
40 }
41 let tree = encode_tree(closure, &mut intern)?;
42 // 44 bytes is never a valid legacy count + hashes row (4 mod 32).
43 let mut root = Vec::with_capacity(44);
44 root.extend_from_slice(MAGIC);
45 root.extend_from_slice(&count.to_le_bytes());
46 root.extend_from_slice(&tree);
47 Ok(root)
48}
49
50pub(crate) fn is_shared(bytes: &[u8]) -> bool {
51 bytes.len() == 44 && bytes.starts_with(MAGIC)

Calls 6

encode_treeFunction · 0.85
to_le_bytesMethod · 0.80
invalidFunction · 0.70
lenMethod · 0.45
allMethod · 0.45
as_bytesMethod · 0.45