dh

Trait Rw

source
pub trait Rw<'a>
where Self: Readable<'a> + Writable<'a>,
{ // Required methods fn rw_as_trait(&mut self) -> &mut dyn Rw<'a>; fn rw_close(self) -> Result<Option<DataType<'a>>>; // Provided method fn rw_limit(&'a mut self, start: u64, length: u64) -> Result<RwLimited<'a>> { ... } }
Expand description

Provides methods to combine the Readable and Writable traits.

Required Methods§

source

fn rw_as_trait(&mut self) -> &mut dyn Rw<'a>

An internal method to get the reader as a trait object. Yes, this is kinda nonsense, but Rust forces me into that.

§How you implement it
fn rw_as_trait(&mut self) -> &mut dyn Rw<'a> {
    self
}
source

fn rw_close(self) -> Result<Option<DataType<'a>>>

Closes the R/W stream and can return the target if it was moved or references it.

Use this instead of Readable::close or Writable::close for cleaner code as you avoid the naming conflict.

Provided Methods§

source

fn rw_limit(&'a mut self, start: u64, length: u64) -> Result<RwLimited<'a>>

Limits the R/W stream to a certain range.

Using Readable::limit and Writable::limit would not return RwLimited and would be limited to either reading or writing.

§Example
use dh::recommended::*;

let mut data = vec![0, 1, 2, 3];
let mut rw = dh::data::rw_ref(&mut data);

let mut limited = rw.rw_limit(1, 2).unwrap();
limited.to(1).unwrap();
limited.write_u8(4).unwrap();

limited.rewind().unwrap();
assert_eq!(limited.read_u16be().unwrap(), 0x0104);

Implementors§

source§

impl<'a> Rw<'a> for RwData

source§

impl<'a> Rw<'a> for RwRefData<'a>

source§

impl<'a> Rw<'a> for RwFile

source§

impl<'a> Rw<'a> for RwLimited<'a>