Skip to content

Commit 5a5a4e2

Browse files
committed
refactor: Value constructors
Use from_byte_array when possible. This should be faster than a deep recursion into ever smaller Value constructors. The code is also more concise. Update documentation.
1 parent a3fe129 commit 5a5a4e2

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/value.rs

+43-39
Original file line numberDiff line numberDiff line change
@@ -170,72 +170,76 @@ impl Value {
170170
}
171171
}
172172

173-
/// Encode a single bit as a value. Will panic if the input is out of range
174-
pub fn u1(n: u8) -> Self {
175-
match n {
173+
/// Create a 1-bit integer.
174+
///
175+
/// ## Panics
176+
///
177+
/// The value is out of range.
178+
pub fn u1(value: u8) -> Self {
179+
match value {
176180
0 => Self::left(Self::unit(), Final::unit()),
177181
1 => Self::right(Final::unit(), Self::unit()),
178182
x => panic!("{} out of range for Value::u1", x),
179183
}
180184
}
181185

182-
/// Encode a two-bit number as a value. Will panic if the input is out of range
183-
pub fn u2(n: u8) -> Self {
184-
let b0 = (n & 2) / 2;
185-
let b1 = n & 1;
186-
assert!(n <= 3, "{} out of range for Value::u2", n);
186+
/// Create a 2-bit integer.
187+
///
188+
/// ## Panics
189+
///
190+
/// The value is out of range.
191+
pub fn u2(value: u8) -> Self {
192+
let b0 = (value & 2) / 2;
193+
let b1 = value & 1;
194+
assert!(value <= 3, "{} out of range for Value::u2", value);
187195
Self::product(Self::u1(b0), Self::u1(b1))
188196
}
189197

190-
/// Encode a four-bit number as a value. Will panic if the input is out of range
191-
pub fn u4(n: u8) -> Self {
192-
let w0 = (n & 12) / 4;
193-
let w1 = n & 3;
194-
assert!(n <= 15, "{} out of range for Value::u2", n);
198+
/// Create a 4-bit integer.
199+
///
200+
/// ## Panics
201+
///
202+
/// The value is ouf of range.
203+
pub fn u4(value: u8) -> Self {
204+
let w0 = (value & 12) / 4;
205+
let w1 = value & 3;
206+
assert!(value <= 15, "{} out of range for Value::u2", value);
195207
Self::product(Self::u2(w0), Self::u2(w1))
196208
}
197209

198-
/// Encode an eight-bit number as a value
199-
pub fn u8(n: u8) -> Self {
200-
let w0 = n >> 4;
201-
let w1 = n & 0xf;
210+
/// Create an 8-bit integer.
211+
pub fn u8(value: u8) -> Self {
212+
let w0 = value >> 4;
213+
let w1 = value & 0xf;
202214
Self::product(Self::u4(w0), Self::u4(w1))
203215
}
204216

205-
/// Encode a 16-bit number as a value
206-
pub fn u16(n: u16) -> Self {
207-
let w0 = (n >> 8) as u8;
208-
let w1 = (n & 0xff) as u8;
209-
Self::product(Self::u8(w0), Self::u8(w1))
217+
/// Create a 16-bit integer.
218+
pub fn u16(bytes: u16) -> Self {
219+
Self::from_byte_array(bytes.to_be_bytes())
210220
}
211221

212-
/// Encode a 32-bit number as a value
213-
pub fn u32(n: u32) -> Self {
214-
let w0 = (n >> 16) as u16;
215-
let w1 = (n & 0xffff) as u16;
216-
Self::product(Self::u16(w0), Self::u16(w1))
222+
/// Create a 32-bit integer.
223+
pub fn u32(bytes: u32) -> Self {
224+
Self::from_byte_array(bytes.to_be_bytes())
217225
}
218226

219-
/// Encode a 64-bit number as a value
220-
pub fn u64(n: u64) -> Self {
221-
let w0 = (n >> 32) as u32;
222-
let w1 = (n & 0xffff_ffff) as u32;
223-
Self::product(Self::u32(w0), Self::u32(w1))
227+
/// Create a 64-bit integer.
228+
pub fn u64(bytes: u64) -> Self {
229+
Self::from_byte_array(bytes.to_be_bytes())
224230
}
225231

226-
/// Encode a 128-bit number as a value
227-
pub fn u128(n: u128) -> Self {
228-
let w0 = (n >> 64) as u64;
229-
let w1 = n as u64; // Cast safety: picking last 64 bits
230-
Self::product(Self::u64(w0), Self::u64(w1))
232+
/// Create a 128-bit integer.
233+
pub fn u128(bytes: u128) -> Self {
234+
Self::from_byte_array(bytes.to_be_bytes())
231235
}
232236

233-
/// Create a value from 32 bytes.
237+
/// Create a 256-bit integer.
234238
pub fn u256(bytes: [u8; 32]) -> Self {
235239
Self::from_byte_array(bytes)
236240
}
237241

238-
/// Create a value from 64 bytes.
242+
/// Create a 512-bit integer.
239243
pub fn u512(bytes: [u8; 64]) -> Self {
240244
Self::from_byte_array(bytes)
241245
}

0 commit comments

Comments
 (0)