Struct bao::encode::SliceExtractor
source · [−]Expand description
An incremental slice extractor, which reads encoded bytes and produces a slice.
SliceExtractor
supports reading both the combined and outboard encoding, depending on which
constructor you use. Though to be clear, there’s no such thing as an “outboard slice” per se.
Slices always include subtree hashes inline with the content, as a combined encoding does.
Note that slices always split the encoding at chunk boundaries. The BLAKE3 chunk size is 1024
bytes, so using slice_start
and slice_len
values that are an even multiple of 1024 avoids
wasting space.
Extracting a slice doesn’t re-hash any of the bytes. As a result, it’s fast compared to
decoding. You can quickly convert an outboard encoding to a combined encoding by “extracting” a
slice with a slice_start
of zero and a slice_len
equal to the original input length.
See the decode
module for decoding slices.
Example
use std::io::prelude::*;
let input = vec![0; 1_000_000];
let (encoded, hash) = bao::encode::encode(&input);
// These parameters are multiples of the chunk size, which avoids unnecessary overhead.
let slice_start = 65536;
let slice_len = 8192;
let encoded_cursor = std::io::Cursor::new(&encoded);
let mut extractor = bao::encode::SliceExtractor::new(encoded_cursor, slice_start, slice_len);
let mut slice = Vec::new();
extractor.read_to_end(&mut slice)?;
// The slice includes some overhead to store the necessary subtree hashes.
assert_eq!(9096, slice.len());
Implementations
sourceimpl<T: Read + Seek> SliceExtractor<T, T>
impl<T: Read + Seek> SliceExtractor<T, T>
sourcepub fn new(input: T, slice_start: u64, slice_len: u64) -> Self
pub fn new(input: T, slice_start: u64, slice_len: u64) -> Self
Create a new SliceExtractor
to read from a combined encoding. Note that slice_start
and
slice_len
are with respect to the content of the encoding, that is, the original
input bytes. This corresponds to bao slice slice_start slice_len
.
sourceimpl<T: Read + Seek, O: Read + Seek> SliceExtractor<T, O>
impl<T: Read + Seek, O: Read + Seek> SliceExtractor<T, O>
sourcepub fn new_outboard(
input: T,
outboard: O,
slice_start: u64,
slice_len: u64
) -> Self
pub fn new_outboard(
input: T,
outboard: O,
slice_start: u64,
slice_len: u64
) -> Self
Create a new SliceExtractor
to read from an unmodified input file and an outboard
encoding of that same file (see Encoder::new_outboard
). As with SliceExtractor::new
,
slice_start
and slice_len
are with respect to the content of the encoding, that is,
the original input bytes. This corresponds to bao slice slice_start slice_len --outboard
.
sourcepub fn into_inner(self) -> (T, Option<O>)
pub fn into_inner(self) -> (T, Option<O>)
Return the underlying readers. The second reader is Some
if and only if this
SliceExtractor
was created with new_outboard
.
Trait Implementations
sourceimpl<T: Read + Seek, O: Read + Seek> Read for SliceExtractor<T, O>
impl<T: Read + Seek, O: Read + Seek> Read for SliceExtractor<T, O>
sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like read
, except that it reads into a slice of buffers. Read more
sourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
1.0.0 · sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
1.0.0 · sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
sourcefn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Pull some bytes from this source into the specified buffer. Read more
sourcefn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Read the exact number of bytes required to fill buf
. Read more
1.0.0 · sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read
. Read more
Auto Trait Implementations
impl<T, O> RefUnwindSafe for SliceExtractor<T, O> where
O: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, O> Send for SliceExtractor<T, O> where
O: Send,
T: Send,
impl<T, O> Sync for SliceExtractor<T, O> where
O: Sync,
T: Sync,
impl<T, O> Unpin for SliceExtractor<T, O> where
O: Unpin,
T: Unpin,
impl<T, O> UnwindSafe for SliceExtractor<T, O> where
O: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more