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> { 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"); db::Reader::new(NEW_DB_FILENAME, 2048).expect("new db reader");
let mut questions = reader.iter(); let mut questions = reader.iter();

View File

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