reader without mut

This commit is contained in:
Dmitry Belyaev 2023-03-28 14:59:44 +03:00
parent 249ac3a4ef
commit e18539a982
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
2 changed files with 10 additions and 9 deletions

View File

@ -232,7 +232,7 @@ fn main() {
}
fn read_from_db2(id: u32) -> Option<Question> {
let mut reader: db::Reader<Question> =
let reader: db::Reader<Question> =
db::Reader::new(NEW_DB_FILENAME, 2048).expect("new db reader");
let mut questions = reader.iter();

View File

@ -3,6 +3,7 @@ use std::{
io::{self, Cursor, Read, Write},
marker::PhantomData,
path::Path,
sync::Arc
};
use memmap::{Mmap, MmapOptions};
@ -157,7 +158,7 @@ where
mmap: Mmap,
count: usize,
first_pos: LSize,
_t: PhantomData<*const T>,
_t: Option<Arc<T>> // PhantomData replacement
}
impl<T> Reader<T>
@ -178,7 +179,7 @@ where
mmap,
count,
first_pos,
_t: PhantomData,
_t: None
})
}
@ -190,7 +191,7 @@ where
0 == self.len()
}
pub fn get(&mut self, index: usize) -> Result<T, String> {
pub fn get(&self, index: usize) -> Result<T, String> {
if index >= self.len() {
return Err("index out of range".into());
}
@ -222,7 +223,7 @@ where
Ok(item.0)
}
pub fn iter(&mut self) -> ReaderIter<'_, T> {
pub fn iter(&self) -> ReaderIter<'_, T> {
ReaderIter::new(self)
}
}
@ -231,7 +232,7 @@ pub struct ReaderIter<'a, T>
where
T: bincode::Decode,
{
reader: &'a mut Reader<T>,
reader: &'a Reader<T>,
index: Option<usize>,
}
@ -239,7 +240,7 @@ impl<'a, T> ReaderIter<'a, T>
where
T: bincode::Decode,
{
fn new(reader: &'a mut Reader<T>) -> Self {
fn new(reader: &'a Reader<T>) -> Self {
ReaderIter {
reader,
index: None,
@ -345,7 +346,7 @@ mod test {
writer.load(&mut items.clone().into_iter()).expect("load");
writer.finish().expect("finish write");
let mut reader: Reader<TestData> = Reader::new(&tmpfile, 2048).expect("new reader");
let reader: Reader<TestData> = Reader::new(&tmpfile, 2048).expect("new reader");
assert_eq!(items.len(), reader.len());
for (idx, item) in items.iter().enumerate() {
@ -372,7 +373,7 @@ mod test {
writer.load(&mut items.clone().into_iter()).expect("load");
writer.finish().expect("finish write");
let mut reader: Reader<TestData> = Reader::new(&tmpfile, 2048).expect("new reader");
let reader: Reader<TestData> = Reader::new(&tmpfile, 2048).expect("new reader");
assert_eq!(items.len(), reader.len());
items.into_iter().zip(reader.iter()).for_each(|pair| {