Skip to content

Commit 875c455

Browse files
committed
make key length const generic
1 parent 0ebe426 commit 875c455

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use core::borrow::Borrow;
66

77
/// Trait for providers of append-only random-access log functionality.
8-
pub trait Aora<K: Into<[u8; 32]> + From<[u8; 32]>, V> {
8+
pub trait Aora<K: Into<[u8; LEN]> + From<[u8; LEN]>, V, const LEN: usize = 32> {
99
/// Adds item to the append-only log. If the item is already in the log, does noting.
1010
///
1111
/// # Panic
@@ -38,14 +38,14 @@ pub mod file {
3838

3939
use super::*;
4040

41-
pub struct FileAora<Id: Ord + From<[u8; 32]>, T> {
41+
pub struct FileAora<Id: Ord + From<[u8; LEN]>, T, const LEN: usize = 32> {
4242
log: File,
4343
idx: File,
4444
index: BTreeMap<Id, u64>,
4545
_phantom: PhantomData<T>,
4646
}
4747

48-
impl<Id: Ord + From<[u8; 32]>, T> FileAora<Id, T> {
48+
impl<Id: Ord + From<[u8; LEN]>, T, const LEN: usize> FileAora<Id, T, LEN> {
4949
fn prepare(path: impl AsRef<Path>, name: &str) -> (PathBuf, PathBuf) {
5050
let path = path.as_ref();
5151
let log = path.join(format!("{name}.log"));
@@ -83,7 +83,7 @@ pub mod file {
8383

8484
let mut index = BTreeMap::new();
8585
loop {
86-
let mut id = [0u8; 32];
86+
let mut id = [0u8; LEN];
8787
let res = idx.read_exact(&mut id);
8888
if matches!(res, Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof) {
8989
break;
@@ -108,8 +108,11 @@ pub mod file {
108108
}
109109
}
110110

111-
impl<Id: Ord + From<[u8; 32]> + Into<[u8; 32]>, T: Eq + StrictEncode + StrictDecode> Aora<Id, T>
112-
for FileAora<Id, T>
111+
impl<
112+
Id: Ord + From<[u8; LEN]> + Into<[u8; LEN]>,
113+
T: Eq + StrictEncode + StrictDecode,
114+
const LEN: usize,
115+
> Aora<Id, T, LEN> for FileAora<Id, T, LEN>
113116
{
114117
fn append(&mut self, id: Id, item: &T) {
115118
if self.has(&id) {
@@ -167,17 +170,17 @@ pub mod file {
167170
}
168171
}
169172

170-
pub struct Iter<'file, Id: From<[u8; 32]>, T: StrictDecode> {
173+
pub struct Iter<'file, Id: From<[u8; LEN]>, T: StrictDecode, const LEN: usize> {
171174
log: StrictReader<StreamReader<&'file File>>,
172175
idx: &'file File,
173176
_phantom: PhantomData<(Id, T)>,
174177
}
175178

176-
impl<Id: From<[u8; 32]>, T: StrictDecode> Iterator for Iter<'_, Id, T> {
179+
impl<Id: From<[u8; LEN]>, T: StrictDecode, const LEN: usize> Iterator for Iter<'_, Id, T, LEN> {
177180
type Item = (Id, T);
178181

179182
fn next(&mut self) -> Option<Self::Item> {
180-
let mut id = [0u8; 32];
183+
let mut id = [0u8; LEN];
181184
self.idx.read_exact(&mut id).ok()?;
182185
self.idx
183186
.seek(SeekFrom::Current(8))

0 commit comments

Comments
 (0)