@@ -39,20 +39,20 @@ fn poly_mod(mut c: u64, val: u64) -> u64 {
39
39
c
40
40
}
41
41
42
- /// Compute the checksum of a descriptor
43
- /// Note that this function does not check if the
44
- /// descriptor string is syntactically correct or not.
45
- /// This only computes the checksum
42
+ /// Compute the checksum of a descriptor.
43
+ ///
44
+ /// Note that this function does not check if the descriptor string is
45
+ /// syntactically correct or not. This only computes the checksum.
46
46
pub fn desc_checksum ( desc : & str ) -> Result < String , Error > {
47
47
let mut eng = Engine :: new ( ) ;
48
48
eng. input ( desc) ?;
49
49
Ok ( eng. checksum ( ) )
50
50
}
51
51
52
- /// Helper function for FromStr for various
53
- /// descriptor types. Checks and verifies the checksum
54
- /// if it is present and returns the descriptor string
55
- /// without the checksum
52
+ /// Helper function for ` FromStr` for various descriptor types.
53
+ ///
54
+ /// Checks and verifies the checksum if it is present and returns the descriptor
55
+ /// string without the checksum.
56
56
pub ( super ) fn verify_checksum ( s : & str ) -> Result < & str , Error > {
57
57
for ch in s. as_bytes ( ) {
58
58
if * ch < 20 || * ch > 127 {
@@ -74,7 +74,7 @@ pub(super) fn verify_checksum(s: &str) -> Result<&str, Error> {
74
74
Ok ( desc_str)
75
75
}
76
76
77
- /// An engine to compute a checksum from a string
77
+ /// An engine to compute a checksum from a string.
78
78
pub struct Engine {
79
79
c : u64 ,
80
80
cls : u64 ,
@@ -86,10 +86,10 @@ impl Default for Engine {
86
86
}
87
87
88
88
impl Engine {
89
- /// Construct an engine with no input
89
+ /// Constructs an engine with no input.
90
90
pub fn new ( ) -> Self { Engine { c : 1 , cls : 0 , clscount : 0 } }
91
91
92
- /// Checksum some data
92
+ /// Inputs some data into the checksum engine.
93
93
///
94
94
/// If this function returns an error, the `Engine` will be left in an indeterminate
95
95
/// state! It is safe to continue feeding it data but the result will not be meaningful.
@@ -115,7 +115,8 @@ impl Engine {
115
115
Ok ( ( ) )
116
116
}
117
117
118
- /// Obtain the checksum of all the data thus-far fed to the engine
118
+ /// Obtains the checksum characters of all the data thus-far fed to the
119
+ /// engine without allocating, to get a string use [`Self::checksum`].
119
120
pub fn checksum_chars ( & mut self ) -> [ char ; 8 ] {
120
121
if self . clscount > 0 {
121
122
self . c = poly_mod ( self . c , self . cls ) ;
@@ -130,23 +131,23 @@ impl Engine {
130
131
chars
131
132
}
132
133
133
- /// Obtain the checksum of all the data thus-far fed to the engine
134
+ /// Obtains the checksum of all the data thus-far fed to the engine.
134
135
pub fn checksum ( & mut self ) -> String {
135
136
String :: from_iter ( self . checksum_chars ( ) . iter ( ) . copied ( ) )
136
137
}
137
138
}
138
139
139
- /// A wrapper around a `fmt::Formatter` which provides checksumming ability
140
+ /// A wrapper around a `fmt::Formatter` which provides checksumming ability.
140
141
pub struct Formatter < ' f , ' a > {
141
142
fmt : & ' f mut fmt:: Formatter < ' a > ,
142
143
eng : Engine ,
143
144
}
144
145
145
146
impl < ' f , ' a > Formatter < ' f , ' a > {
146
- /// Contruct a new `Formatter`, wrapping a given `fmt::Formatter`
147
+ /// Contructs a new `Formatter`, wrapping a given `fmt::Formatter`.
147
148
pub fn new ( f : & ' f mut fmt:: Formatter < ' a > ) -> Self { Formatter { fmt : f, eng : Engine :: new ( ) } }
148
149
149
- /// Writes the checksum into the underlying `fmt::Formatter`
150
+ /// Writes the checksum into the underlying `fmt::Formatter`.
150
151
pub fn write_checksum ( & mut self ) -> fmt:: Result {
151
152
use fmt:: Write ;
152
153
self . fmt . write_char ( '#' ) ?;
@@ -156,7 +157,7 @@ impl<'f, 'a> Formatter<'f, 'a> {
156
157
Ok ( ( ) )
157
158
}
158
159
159
- /// Writes the checksum into the underlying `fmt::Formatter`, unless it has "alternate" display on
160
+ /// Writes the checksum into the underlying `fmt::Formatter`, unless it has "alternate" display on.
160
161
pub fn write_checksum_if_not_alt ( & mut self ) -> fmt:: Result {
161
162
if !self . fmt . alternate ( ) {
162
163
self . write_checksum ( ) ?;
0 commit comments