Struct bao::decode::SliceDecoder
source · [−]pub struct SliceDecoder<T: Read> { /* private fields */ }
Expand description
An incremental slice decoder. This reads and verifies the output of the
SliceExtractor
.
Note that there is no such thing as an “outboard slice”. All slices include the content bytes and tree nodes intermixed, as in the combined encoding mode.
Example
use std::io::prelude::*;
// Start by encoding some input.
let input = vec![0; 1_000_000];
let (encoded, hash) = bao::encode::encode(&input);
// Slice the encoding. 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)?;
// Decode the slice. The result should be the same as the part of the input that the slice
// represents. Note that we're using the same hash that encoding produced, which is
// independent of the slice parameters. That's the whole point; if we just wanted to re-encode
// a portion of the input and wind up with a different hash, we wouldn't need slicing.
let mut decoded = Vec::new();
let mut decoder = bao::decode::SliceDecoder::new(&*slice, &hash, slice_start, slice_len);
decoder.read_to_end(&mut decoded)?;
assert_eq!(&input[slice_start as usize..][..slice_len as usize], &*decoded);
// Like regular decoding, slice decoding will fail if the hash doesn't match.
let mut bad_slice = slice.clone();
let last_index = bad_slice.len() - 1;
bad_slice[last_index] ^= 1;
let mut decoder = bao::decode::SliceDecoder::new(&*bad_slice, &hash, slice_start, slice_len);
let err = decoder.read_to_end(&mut Vec::new()).unwrap_err();
assert_eq!(std::io::ErrorKind::InvalidData, err.kind());
Implementations
sourceimpl<T: Read> SliceDecoder<T>
impl<T: Read> SliceDecoder<T>
Trait Implementations
sourceimpl<T: Read> Read for SliceDecoder<T>
impl<T: Read> Read for SliceDecoder<T>
sourcefn read(&mut self, output: &mut [u8]) -> Result<usize>
fn read(&mut self, output: &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> RefUnwindSafe for SliceDecoder<T> where
T: RefUnwindSafe,
impl<T> Send for SliceDecoder<T> where
T: Send,
impl<T> Sync for SliceDecoder<T> where
T: Sync,
impl<T> Unpin for SliceDecoder<T> where
T: Unpin,
impl<T> UnwindSafe for SliceDecoder<T> where
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