[−][src]Struct pigeon::Writer
A type which can be used for writing to a type that implements Target
.
Implementations
impl<T: Target> Writer<T>
[src]
pub fn new(buf: T) -> Writer<T>
[src]
Create a new Writer
from a type implementing Target
.
Examples
let mut buf = [0; 8]; let mut writer = Writer::new(&mut buf[..]); writer.write(12u8).unwrap(); writer.write(24u8).unwrap(); writer.write(554u16).unwrap(); writer.write(12345678u32).unwrap(); 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 Writer
in its buffer.
Examples
let mut buf = [0; 9]; let mut writer = Writer::new(&mut buf[..]); assert_eq!(writer.position(), 0); writer.write(0u8).unwrap(); assert_eq!(writer.position(), 1); writer.write(0u16).unwrap(); assert_eq!(writer.position(), 3); writer.write_bytes(b"coucou").unwrap(); assert_eq!(writer.position(), 9);
pub fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>
[src]
Write a byte slice into the Writer
at the current offset.
Position
This advances the writer only if the write succeeds.
No data will be written if the write does not succeed.
Returns
On success, this returns Ok(())
.
If there is not enough space to write the whole buffer, this returns
Err(Error::UnexpectedEof)
.
Examples
let mut buf = [0; 13]; let mut writer = Writer::new(&mut buf[..]); writer.write_bytes(b"Hello, world!").unwrap(); assert_eq!(&buf, b"Hello, world!");
pub fn write<D: Pack>(&mut self, data: D) -> Result<()>
[src]
Write a type implementing Pack
into the Writer
at the current offset.
Position
This advances the writer only if all the underlying writes succeed.
On failure, there may be incomplete data written to the buffer, the writer's position will not include that data.
Returns
On success, this returns an Ok(())
.
If there is not enough space to write all the data, this returns Err(Error::UnexpectedEof)
.
Examples
let mut buf = [0; 14]; let mut writer = Writer::new(&mut buf[..]); writer.write(ShortStr("Hello, world!")).unwrap(); assert_eq!(&buf, b"\x0DHello, world!");
pub fn fits<D: Pack>(&mut self, data: D) -> bool
[src]
Check whether a type implementing Pack
fits into the rest of the buffer.
Examples
let mut buf = [0; 4]; let mut writer = Writer::new(&mut buf[..]); assert!(writer.fits(0u32)); assert!(!writer.fits(0u64)); writer.write(0u8).unwrap(); assert!(writer.fits(0u16)); assert!(!writer.fits(0u32)); writer.write(0u16).unwrap(); assert!(writer.fits(0u8)); assert!(!writer.fits(0u16)); writer.write(0u8).unwrap(); 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 = Writer::new(&mut buf[..]); assert!(writer.fits_bytes(b"hewo")); assert!(!writer.fits_bytes(b"hallo, iedereen!")); writer.write_bytes(b"mew").unwrap(); assert!(writer.fits_bytes(&[12])); assert!(!writer.fits_bytes(b"hewo"));
pub fn with(
buf: T,
cb: impl FnOnce(&mut Writer<T>) -> Result<()>
) -> Result<usize>
[src]
buf: T,
cb: impl FnOnce(&mut Writer<T>) -> Result<()>
) -> Result<usize>
Create a Writer
on a buffer and pass it into a closure.
Position
This advances the writer only if all the underlying writes succeed.
On failure, there may be incomplete data written to the buffer, the writer's position will not include that data.
Returns
On success, this returns an Ok
with the amount of bytes written.
If there is not enough space to write all the data, this returns Err(Error::UnexpectedEof)
.
Examples
let mut buf = [0; 32]; let size = Writer::with(&mut buf[..], |writer| { writer.write(ShortStr("bonjour, tout le monde!"))?; writer.write(12u8)?; Ok(()) }).unwrap(); assert_eq!(&buf[0..size], b"\x17bonjour, tout le monde!\x0C");
impl<'a> Writer<&'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 = Writer::new(&mut buf[..]); writer.write_bytes(b"ik hou van je!"); assert_eq!(writer.as_bytes(), b"ik hou van je!");
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Writer<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for Writer<T> where
T: Send,
T: Send,
impl<T> Sync for Writer<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Writer<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Writer<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,