Skip to content

Commit 6c51b91

Browse files
committed
added ReaderInner trait to allow trait object inside Reader + doc updates
1 parent 3f82e6b commit 6c51b91

4 files changed

Lines changed: 43 additions & 16 deletions

File tree

diskann-record/src/backend/disk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl LoadContext for DiskLoadContext {
249249
load::Error::new(err).context(format!("while opening artifact file {}", full.display()))
250250
})?;
251251

252-
Ok(Reader::new(Box::new(file)))
252+
Ok(Reader::new(file))
253253
}
254254
}
255255

diskann-record/src/backend/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl LoadContext for MemoryContext {
144144

145145
fn read(&self, key: &str) -> load::Result<Reader<'_>> {
146146
match self.files.get(key) {
147-
Some(bytes) => Ok(Reader::new(Box::new(Cursor::new(bytes.as_slice())))),
147+
Some(bytes) => Ok(Reader::new(Cursor::new(bytes.as_slice()))),
148148
None => Err(
149149
load::Error::from(load::error::Kind::MissingFile).context(format!(
150150
"handle references artifact {:?} which is not registered in this context",

diskann-record/src/load/context.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
//! * [`Object::read`] for side-car artifacts referenced by a
1616
//! [`save::Handle`](super::save::Handle).
1717
//!
18-
//! [`Reader`] implements [`std::io::Read`] over a side-car artifact, regardless of the
19-
//! provider's backing store.
18+
//! [`Reader`] implements [`std::io::Read`] and [`std::io::Seek`] over a side-car
19+
//! artifact, regardless of the provider's backing store.
2020
2121
use std::io::BufReader;
2222

@@ -30,7 +30,7 @@ use crate::{
3030
///
3131
/// A `LoadContext` supplies the root manifest [`save::Value`] ([`LoadContext::value`])
3232
/// and resolves side-car artifacts referenced by handles ([`LoadContext::read`]). The
33-
/// concrete implementations live under [`crate::backend`]: a disk-backed context (under
33+
/// concrete implementations live under `crate::backend`: a disk-backed context (under
3434
/// the `disk` feature) and an in-memory context. Alternative implementations (e.g. a
3535
/// virtual filesystem) can be supplied for testing.
3636
///
@@ -54,23 +54,35 @@ pub trait LoadContext {
5454
fn read(&self, key: &str) -> Result<Reader<'_>>;
5555
}
5656

57+
/// The backend-specific half of a [`Reader`].
58+
///
59+
/// Each [`LoadContext`] implementation supplies its own `ReaderInner` (e.g. an in-memory
60+
/// cursor or an on-disk file). The blanket impl covers any type that is both
61+
/// [`std::io::Read`] and [`std::io::Seek`].
62+
pub(crate) trait ReaderInner: std::io::Read + std::io::Seek {}
63+
64+
impl<T> ReaderInner for T where T: std::io::Read + std::io::Seek {}
65+
5766
/// A borrowed reader over a side-car artifact.
5867
///
59-
/// Produced by [`Object::read`]. Implements [`std::io::Read`] over whatever backing
60-
/// store the [`LoadContext`] provides, so non-file-backed providers (like an in-memory byte buffer) can supply an
61-
/// arbitrary [`std::io::Read`].
68+
/// Produced by [`Object::read`]. Implements [`std::io::Read`] and [`std::io::Seek`] over
69+
/// whatever backing store the [`LoadContext`] provides, so non-file-backed providers
70+
/// (like an in-memory byte buffer) can supply an arbitrary seekable reader.
6271
pub struct Reader<'a> {
63-
io: BufReader<Box<dyn std::io::Read + 'a>>,
72+
io: BufReader<Box<dyn ReaderInner + 'a>>,
6473
}
6574

6675
impl<'a> Reader<'a> {
67-
/// Build a reader over an arbitrary borrowed [`std::io::Read`] source.
76+
/// Build a reader over an arbitrary borrowed [`ReaderInner`] source.
6877
///
69-
/// Used by non-file-backed [`LoadContext`] implementations (e.g. the in-memory
70-
/// context) to expose a side-car artifact backed by a [`std::io::Cursor`].
71-
pub(crate) fn new(io: Box<dyn std::io::Read + 'a>) -> Self {
78+
/// Used by [`LoadContext`] implementations to expose a side-car artifact backed by a
79+
/// file or an in-memory [`std::io::Cursor`].
80+
pub(crate) fn new<T>(io: T) -> Self
81+
where
82+
T: ReaderInner + 'a,
83+
{
7284
Self {
73-
io: BufReader::new(io),
85+
io: BufReader::new(Box::new(io)),
7486
}
7587
}
7688
}
@@ -96,6 +108,21 @@ impl std::io::Read for Reader<'_> {
96108
}
97109
}
98110

111+
impl std::io::Seek for Reader<'_> {
112+
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
113+
self.io.seek(pos)
114+
}
115+
fn rewind(&mut self) -> std::io::Result<()> {
116+
self.io.rewind()
117+
}
118+
fn stream_position(&mut self) -> std::io::Result<u64> {
119+
self.io.stream_position()
120+
}
121+
fn seek_relative(&mut self, offset: i64) -> std::io::Result<()> {
122+
self.io.seek_relative(offset)
123+
}
124+
}
125+
99126
///////////////////////
100127
// User facing types //
101128
///////////////////////

diskann-record/src/save/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) use delegate_write_and_seek;
6363
///
6464
/// A `SaveContext` decides where side-car artifacts are written ([`SaveContext::write`])
6565
/// and how the final manifest is committed ([`SaveContext::finish`]). The concrete
66-
/// implementations live under [`crate::backend`]: a disk-backed context (under the `disk`
66+
/// implementations live under `crate::backend`: a disk-backed context (under the `disk`
6767
/// feature) and an in-memory context. Alternative implementations (e.g. a virtual
6868
/// filesystem) can be supplied for testing or to avoid touching the filesystem.
6969
///
@@ -165,7 +165,7 @@ pub(crate) trait WriterInner: std::io::Write + std::io::Seek + std::fmt::Debug {
165165
///
166166
/// Implements [`std::io::Write`] and [`std::io::Seek`]. Writes are buffered; calling
167167
/// [`Writer::finish`] flushes the buffer, commits the artifact through the backing
168-
/// [`WriterInner`], and returns a [`Handle`].
168+
/// writer, and returns a [`Handle`].
169169
#[derive(Debug)]
170170
pub struct Writer<'a> {
171171
inner: BufWriter<Box<dyn WriterInner + 'a>>,

0 commit comments

Comments
 (0)