Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

io.rs 1.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  1. use std::io;
  2. use sha1::Sha1;
  3. /// Write wrapper that computes Sha1 checksums of the data written.
  4. pub struct HashWrite<'a, W: io::Write> {
  5. writer: W,
  6. hash: &'a mut Sha1
  7. }
  8. impl <'a, W: io::Write> HashWrite<'a, W> {
  9. /// Create a hash writer
  10. pub fn create(base: W, hash: &'a mut Sha1) -> HashWrite<'a, W> {
  11. HashWrite {
  12. writer: base,
  13. hash: hash
  14. }
  15. }
  16. }
  17. impl <'a, W: io::Write> io::Write for HashWrite<'a, W> {
  18. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  19. self.hash.update(buf);
  20. self.writer.write(buf)
  21. }
  22. fn flush(&mut self) -> io::Result<()> {
  23. self.writer.flush()
  24. }
  25. }
  26. /// Read wrapper that computes Sha1 checksums of the data read.
  27. pub struct HashRead<'a, R: io::Read> {
  28. reader: R,
  29. hash: &'a mut Sha1
  30. }
  31. impl <'a, R: io::Read> HashRead<'a, R> {
  32. /// Create a hash reader
  33. pub fn create(base: R, hash: &'a mut Sha1) -> HashRead<'a, R> {
  34. HashRead {
  35. reader: base,
  36. hash: hash
  37. }
  38. }
  39. }
  40. impl <'a, R: io::Read> io::Read for HashRead<'a, R> {
  41. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  42. let n = self.reader.read(buf)?;
  43. self.hash.update(&buf[0..n]);
  44. Ok(n)
  45. }
  46. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...