[][src]Struct pigeon::FrameWriter

pub struct FrameWriter<T: FrameTarget> { /* fields omitted */ }

A type which can be used for writing to a type that implements FrameTarget.

Implementations

impl<T: FrameTarget> FrameWriter<T>[src]

pub fn new(buf: T) -> FrameWriter<T>[src]

Create a new FrameWriter from a type implementing FrameTarget.

Examples

let mut buf = [0; 8];
let mut writer = FrameWriter::new(&mut buf[..]);

writer.write(12u8);
writer.write(24u8);
writer.write(554u16);
writer.write(12345678u32);

assert_eq!(buf[0], 12);
assert_eq!(buf[1], 24);
assert_eq!(u16::from_be_bytes(buf[2..4].try_into().unwrap()), 554);
assert_eq!(u32::from_be_bytes(buf[4..8].try_into().unwrap()), 12345678);

pub fn position(&self) -> usize[src]

Get the position of the FrameWriter in its buffer.

Examples

let mut buf = [0; 9];
let mut writer = FrameWriter::new(&mut buf[..]);

assert_eq!(writer.position(), 0);

writer.write(0u8);

assert_eq!(writer.position(), 1);

writer.write(0u16);

assert_eq!(writer.position(), 3);

writer.write_bytes(b"coucou");

assert_eq!(writer.position(), 9);

pub fn write_bytes(&mut self, bytes: &[u8])[src]

Write a byte slice into the FrameWriter at the current offset.

Panics

This function will panic if the buffer doesn't have enough remaining space to fit all these bytes.

Examples

let mut buf = [0; 13];
let mut writer = FrameWriter::new(&mut buf[..]);

writer.write_bytes(b"Hello, world!");

assert_eq!(&buf, b"Hello, world!");

pub fn write<D: Dump>(&mut self, data: D)[src]

Write a type implementing Dump into the FrameWriter at the current offset.

Panics

This function will panic if the buffer doesn't have enough remaining space to fit the argument.

Examples

let mut buf = [0; 14];
let mut writer = FrameWriter::new(&mut buf[..]);

writer.write(ShortStr("Hello, world!"));

assert_eq!(&buf, b"\x0DHello, world!");

pub fn fits<D: Dump>(&mut self, data: D) -> bool[src]

Check whether a type implementing Dump fits into the rest of the buffer.

Examples

let mut buf = [0; 4];
let mut writer = FrameWriter::new(&mut buf[..]);

assert!(writer.fits(0u32));
assert!(!writer.fits(0u64));

writer.write(0u8);

assert!(writer.fits(0u16));
assert!(!writer.fits(0u32));

writer.write(0u16);

assert!(writer.fits(0u8));
assert!(!writer.fits(0u16));

writer.write(0u8);

assert!(!writer.fits(0u8));

pub fn fits_bytes(&mut self, bytes: &[u8]) -> bool[src]

Check whether a byteslice can fit into the buffer.

Examples

let mut buf = [0; 4];
let mut writer = FrameWriter::new(&mut buf[..]);

assert!(writer.fits_bytes(b"hewo"));
assert!(!writer.fits_bytes(b"hallo, iedereen!"));

writer.write_bytes(b"mew");

assert!(writer.fits_bytes(&[12]));
assert!(!writer.fits_bytes(b"hewo"));

pub fn with(buf: T, cb: impl FnOnce(&mut FrameWriter<T>))[src]

Create a FrameWriter on a buffer and pass it into a closure.

Examples

let mut buf = [0; 25];

FrameWriter::with(&mut buf[..], |writer| {
    writer.write(ShortStr("bonjour, tout le monde!"));
    writer.write(12u8);
});

assert_eq!(&buf, b"\x17bonjour, tout le monde!\x0C");

impl<'a> FrameWriter<&'a mut [u8]>[src]

pub fn as_bytes(&self) -> &[u8][src]

Get a reference to the part of the internal buffer that was written to.

Examples

let mut buf = [0; 32];
let mut writer = FrameWriter::new(&mut buf[..]);

writer.write_bytes(b"ik hou van je!");

assert_eq!(writer.as_bytes(), b"ik hou van je!");

Trait Implementations

impl<T: Debug + FrameTarget> Debug for FrameWriter<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for FrameWriter<T> where
    T: RefUnwindSafe

impl<T> Send for FrameWriter<T> where
    T: Send

impl<T> Sync for FrameWriter<T> where
    T: Sync

impl<T> Unpin for FrameWriter<T> where
    T: Unpin

impl<T> UnwindSafe for FrameWriter<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.