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.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  1. use std::io;
  2. use sha1::Sha1;
  3. use log::*;
  4. /// Write wrapper that computes Sha1 checksums of the data written.
  5. pub struct HashWrite<'a, W: io::Write> {
  6. writer: W,
  7. hash: &'a mut Sha1
  8. }
  9. impl <'a, W: io::Write> HashWrite<'a, W> {
  10. /// Create a hash writer
  11. pub fn create(base: W, hash: &'a mut Sha1) -> HashWrite<'a, W> {
  12. HashWrite {
  13. writer: base,
  14. hash: hash
  15. }
  16. }
  17. }
  18. impl <'a, W: io::Write> io::Write for HashWrite<'a, W> {
  19. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  20. self.hash.update(buf);
  21. self.writer.write(buf)
  22. }
  23. fn flush(&mut self) -> io::Result<()> {
  24. self.writer.flush()
  25. }
  26. }
  27. /// Read wrapper that computes Sha1 checksums of the data read.
  28. pub struct HashRead<'a, R: io::Read> {
  29. reader: R,
  30. hash: &'a mut Sha1
  31. }
  32. impl <'a, R: io::Read> HashRead<'a, R> {
  33. /// Create a hash reader
  34. pub fn create(base: R, hash: &'a mut Sha1) -> HashRead<'a, R> {
  35. HashRead {
  36. reader: base,
  37. hash: hash
  38. }
  39. }
  40. }
  41. impl <'a, R: io::Read> io::Read for HashRead<'a, R> {
  42. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  43. let n = self.reader.read(buf)?;
  44. self.hash.update(&buf[0..n]);
  45. Ok(n)
  46. }
  47. }
  48. pub struct DelimPrinter<'a> {
  49. delim: &'a [u8],
  50. end: &'a [u8],
  51. first: bool
  52. }
  53. impl <'a> DelimPrinter<'a> {
  54. pub fn new(delim: &'a str, end: &'a str) -> DelimPrinter<'a> {
  55. DelimPrinter {
  56. delim: delim.as_bytes(),
  57. end: end.as_bytes(),
  58. first: true
  59. }
  60. }
  61. pub fn preface<W: io::Write>(&mut self, w: &mut W) -> io::Result<bool> {
  62. if self.first {
  63. self.first = false;
  64. Ok(false)
  65. } else {
  66. w.write_all(self.delim)?;
  67. Ok(true)
  68. }
  69. }
  70. pub fn end<W: io::Write>(&mut self, w: &mut W) -> io::Result<()> {
  71. w.write_all(self.end)?;
  72. self.first = true;
  73. Ok(())
  74. }
  75. }
Tip!

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

Comments

Loading...