Skip to content

Commit 5ea3d27

Browse files
Use inherent constants for StreamId
1 parent b749c00 commit 5ea3d27

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

chacha20/src/rng.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,29 +109,32 @@ impl From<u64> for WordPosInput {
109109
}
110110
}
111111

112-
/// Amount of raw bytes backing a `StreamId` instance.
113-
const STREAM_ID_BYTES: usize = size_of::<StreamId>();
114-
/// The length of the array contained within `StreamId`.
115-
const STREAM_ID_LEN: usize = 3;
116-
117112
/// A wrapper for `stream_id`.
118113
/// Can be constructed from any of the following:
119114
/// * `[u32; 3]`
120115
/// * `[u8; 12]`
121116
/// * `u128`
122-
pub struct StreamId([u32; STREAM_ID_LEN]);
117+
pub struct StreamId([u32; Self::LEN]);
118+
119+
impl StreamId {
120+
/// Amount of raw bytes backing a `StreamId` instance.
121+
const BYTES: usize = size_of::<Self>();
122+
123+
/// The length of the array contained within `StreamId`.
124+
const LEN: usize = 3;
125+
}
123126

124-
impl From<[u32; STREAM_ID_LEN]> for StreamId {
127+
impl From<[u32; Self::LEN]> for StreamId {
125128
#[inline]
126-
fn from(value: [u32; STREAM_ID_LEN]) -> Self {
129+
fn from(value: [u32; Self::LEN]) -> Self {
127130
let result = value.map(|v| v.to_le());
128131
Self(result)
129132
}
130133
}
131134

132-
impl From<[u8; STREAM_ID_BYTES]> for StreamId {
135+
impl From<[u8; Self::BYTES]> for StreamId {
133136
#[inline]
134-
fn from(value: [u8; STREAM_ID_BYTES]) -> Self {
137+
fn from(value: [u8; Self::BYTES]) -> Self {
135138
let mut result = Self(Default::default());
136139
for (cur, chunk) in result
137140
.0
@@ -147,8 +150,7 @@ impl From<[u8; STREAM_ID_BYTES]> for StreamId {
147150
impl From<u128> for StreamId {
148151
#[inline]
149152
fn from(value: u128) -> Self {
150-
let result: [u8; STREAM_ID_BYTES] =
151-
value.to_le_bytes()[..STREAM_ID_BYTES].try_into().unwrap();
153+
let result: [u8; Self::BYTES] = value.to_le_bytes()[..Self::BYTES].try_into().unwrap();
152154
result.into()
153155
}
154156
}

0 commit comments

Comments
 (0)