Skip to content

Commit d5900bf

Browse files
committed
Add raw_pos method
1 parent 6d8a512 commit d5900bf

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

sponge-cursor/src/lib.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ impl<Rate: ArraySize> SpongeCursor<Rate> {
5252
}
5353
}
5454

55+
/// Get current cursor position as `u8`.
56+
#[must_use]
57+
#[inline(always)]
58+
pub fn raw_pos(&self) -> u8 {
59+
debug_assert!(self.pos < Rate::U8);
60+
if self.pos < Rate::U8 {
61+
self.pos
62+
} else {
63+
// SAFETY: the type enforces that `pos` is always smaller than `Rate`
64+
unsafe { core::hint::unreachable_unchecked() };
65+
}
66+
}
67+
68+
/// Get current cursor position.
69+
#[must_use]
70+
#[inline(always)]
71+
pub fn pos(&self) -> usize {
72+
let pos = usize::from(self.pos);
73+
debug_assert!(pos < Rate::USIZE);
74+
if pos < Rate::USIZE {
75+
pos
76+
} else {
77+
// SAFETY: the type enforces that `pos` is always smaller than `Rate`
78+
unsafe { core::hint::unreachable_unchecked() };
79+
}
80+
}
81+
5582
/// Absorb bytes from `data` into a `u64`-based state using little ednian byte order.
5683
///
5784
/// Size of state MUST be greater or equal to `Rate`. Using an invalid `N` will result in
@@ -171,20 +198,6 @@ impl<Rate: ArraySize> SpongeCursor<Rate> {
171198

172199
self.pos = u8::try_from(tail.len()).expect("tail.len() is smaller than Rate");
173200
}
174-
175-
/// Get current cursor position.
176-
#[must_use]
177-
#[inline(always)]
178-
pub fn pos(&self) -> usize {
179-
let pos = usize::from(self.pos);
180-
debug_assert!(pos < Rate::USIZE);
181-
if pos < Rate::USIZE {
182-
pos
183-
} else {
184-
// SAFETY: the type enforces that `pos` is always smaller than `Rate`
185-
unsafe { core::hint::unreachable_unchecked() };
186-
}
187-
}
188201
}
189202

190203
#[cfg(feature = "zeroize")]

0 commit comments

Comments
 (0)