From e18539a98256b41c42a1f0df6b5ab967c49ba632 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 28 Mar 2023 14:59:44 +0300 Subject: [PATCH] reader without mut --- app/src/main.rs | 2 +- lib/src/db.rs | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main.rs b/app/src/main.rs index 60969c3..52bd56c 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -232,7 +232,7 @@ fn main() { } fn read_from_db2(id: u32) -> Option { - let mut reader: db::Reader = + let reader: db::Reader = db::Reader::new(NEW_DB_FILENAME, 2048).expect("new db reader"); let mut questions = reader.iter(); diff --git a/lib/src/db.rs b/lib/src/db.rs index f8d9b27..d2b5396 100644 --- a/lib/src/db.rs +++ b/lib/src/db.rs @@ -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> // PhantomData replacement } impl Reader @@ -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 { + pub fn get(&self, index: usize) -> Result { 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, + reader: &'a Reader, index: Option, } @@ -239,7 +240,7 @@ impl<'a, T> ReaderIter<'a, T> where T: bincode::Decode, { - fn new(reader: &'a mut Reader) -> Self { + fn new(reader: &'a Reader) -> 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 = Reader::new(&tmpfile, 2048).expect("new reader"); + let reader: Reader = 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 = Reader::new(&tmpfile, 2048).expect("new reader"); + let reader: Reader = Reader::new(&tmpfile, 2048).expect("new reader"); assert_eq!(items.len(), reader.len()); items.into_iter().zip(reader.iter()).for_each(|pair| {