pub struct Hash(/* private fields */);Expand description
A SHA-256 hash: of a chunk’s bytes, or of the bytes that describe a file, a folder or a tree.
Implementations§
Source§impl Hash
impl Hash
pub const fn from_bytes(bytes: [u8; 32]) -> Self
Sourcepub const fn as_bytes(&self) -> &[u8; 32]
pub const fn as_bytes(&self) -> &[u8; 32]
Examples found in repository?
examples/speed.rs (line 58)
43fn main() -> Result<(), Box<dyn std::error::Error>> {
44 let megabytes: usize = match std::env::args().nth(1) {
45 Some(argument) => argument.parse()?,
46 None => 1024,
47 };
48 let data = filler(megabytes << 20);
49
50 let started = Instant::now();
51 let chunks = Chunking::FastCdcV1.cut(&data);
52 report("chunked", started, data.len());
53 println!("{:<22} {} chunks", "", chunks.len());
54
55 let started = Instant::now();
56 let mut mixed = 0_u8;
57 for chunk in &chunks {
58 mixed ^= Hash::of(chunk).as_bytes()[0];
59 }
60 report("hashed", started, data.len());
61
62 let store = MemoryStore::new();
63 let started = Instant::now();
64 let mut builder = Builder::new(&store, Chunking::FastCdcV1);
65 let file = builder.file_from_bytes(&data)?;
66 report("built", started, data.len());
67
68 let root = builder.finish(file, ExtraData::new())?;
69 let started = Instant::now();
70 verify(&store, &root)?;
71 report("checked", started, data.len());
72 println!("root {root} ({mixed:02x})");
73
74 // The tree is fetched from the first store into a second one, as a
75 // receiver would take it from a peer, and then written as a bundle and
76 // read back into a third. Each checks every byte as it comes.
77 let fetched = MemoryStore::new();
78 let started = Instant::now();
79 fetch(&fetched, &root, &mut Responder::new(&store))?;
80 report("fetched", started, data.len());
81
82 let imported = MemoryStore::new();
83 let mut bundle = Vec::with_capacity(data.len() + (1 << 20));
84 let started = Instant::now();
85 export(&store, &root, &mut bundle)?;
86 import(&imported, &root, &mut bundle.as_slice())?;
87 report("bundled", started, data.len());
88
89 if let Some(folder) = std::env::args().nth(2) {
90 let mut best = f64::MAX;
91 let (mut bytes, mut files) = (0, 0);
92 for _ in 0..5 {
93 let store = MemoryStore::new();
94 let started = Instant::now();
95 let root = build(&store, &folder, &BuildOptions::new())?;
96 best = best.min(started.elapsed().as_secs_f64());
97 bytes = record_of(&store, &root)?.top().size;
98 files = store.count(Stored::FileList);
99 }
100 let rate = bytes as f64 / (1 << 20) as f64 / best.max(1e-9);
101 println!("{:<22} {best:>7.3} s {rate:>8.0} MiB/s", "built from disk");
102 println!("{:<22} {files} different files, {bytes} bytes", "");
103 }
104 Ok(())
105}Sourcepub fn of(bytes: &[u8]) -> Self
pub fn of(bytes: &[u8]) -> Self
The SHA-256 of bytes.
Examples found in repository?
examples/speed.rs (line 58)
43fn main() -> Result<(), Box<dyn std::error::Error>> {
44 let megabytes: usize = match std::env::args().nth(1) {
45 Some(argument) => argument.parse()?,
46 None => 1024,
47 };
48 let data = filler(megabytes << 20);
49
50 let started = Instant::now();
51 let chunks = Chunking::FastCdcV1.cut(&data);
52 report("chunked", started, data.len());
53 println!("{:<22} {} chunks", "", chunks.len());
54
55 let started = Instant::now();
56 let mut mixed = 0_u8;
57 for chunk in &chunks {
58 mixed ^= Hash::of(chunk).as_bytes()[0];
59 }
60 report("hashed", started, data.len());
61
62 let store = MemoryStore::new();
63 let started = Instant::now();
64 let mut builder = Builder::new(&store, Chunking::FastCdcV1);
65 let file = builder.file_from_bytes(&data)?;
66 report("built", started, data.len());
67
68 let root = builder.finish(file, ExtraData::new())?;
69 let started = Instant::now();
70 verify(&store, &root)?;
71 report("checked", started, data.len());
72 println!("root {root} ({mixed:02x})");
73
74 // The tree is fetched from the first store into a second one, as a
75 // receiver would take it from a peer, and then written as a bundle and
76 // read back into a third. Each checks every byte as it comes.
77 let fetched = MemoryStore::new();
78 let started = Instant::now();
79 fetch(&fetched, &root, &mut Responder::new(&store))?;
80 report("fetched", started, data.len());
81
82 let imported = MemoryStore::new();
83 let mut bundle = Vec::with_capacity(data.len() + (1 << 20));
84 let started = Instant::now();
85 export(&store, &root, &mut bundle)?;
86 import(&imported, &root, &mut bundle.as_slice())?;
87 report("bundled", started, data.len());
88
89 if let Some(folder) = std::env::args().nth(2) {
90 let mut best = f64::MAX;
91 let (mut bytes, mut files) = (0, 0);
92 for _ in 0..5 {
93 let store = MemoryStore::new();
94 let started = Instant::now();
95 let root = build(&store, &folder, &BuildOptions::new())?;
96 best = best.min(started.elapsed().as_secs_f64());
97 bytes = record_of(&store, &root)?.top().size;
98 files = store.count(Stored::FileList);
99 }
100 let rate = bytes as f64 / (1 << 20) as f64 / best.max(1e-9);
101 println!("{:<22} {best:>7.3} s {rate:>8.0} MiB/s", "built from disk");
102 println!("{:<22} {files} different files, {bytes} bytes", "");
103 }
104 Ok(())
105}Sourcepub fn from_slice(bytes: &[u8]) -> Result<Self>
pub fn from_slice(bytes: &[u8]) -> Result<Self>
Reads a hash from exactly 32 bytes.
Trait Implementations§
impl Copy for Hash
impl Eq for Hash
Source§impl Ord for Hash
impl Ord for Hash
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Source§impl PartialOrd for Hash
impl PartialOrd for Hash
impl StructuralPartialEq for Hash
Auto Trait Implementations§
impl Forget for Hash
impl Freeze for Hash
impl Move for Hash
impl RefUnwindSafe for Hash
impl Send for Hash
impl Sync for Hash
impl Unpin for Hash
impl UnsafeUnpin for Hash
impl UnwindSafe for Hash
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more