[−][src]Trait tokio_byteorder::AsyncWriteBytesExt
Extends AsyncWrite
with methods for writing numbers.
Most of the methods defined here have an unconstrained type parameter that
must be explicitly instantiated. Typically, it is instantiated with either
the BigEndian
or LittleEndian
types defined in this crate.
Examples
Write unsigned 16 bit big-endian integers to a [Write
]:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = vec![]; wtr.write_u16::<BigEndian>(517).await.unwrap(); wtr.write_u16::<BigEndian>(768).await.unwrap(); assert_eq!(wtr, vec![2, 5, 3, 0]); }
Provided methods
fn write_u8<'a>(&'a mut self, n: u8) -> WriteU8<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes an unsigned 8 bit integer to the underlying writer.
Note that since this writes a single byte, no byte order conversions are used. It is included for completeness.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write unsigned 8 bit integers to a Write
:
use tokio_byteorder::{AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_u8(2).await.unwrap(); wtr.write_u8(5).await.unwrap(); assert_eq!(wtr, b"\x02\x05"); }
fn write_i8<'a>(&'a mut self, n: i8) -> WriteI8<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a signed 8 bit integer to the underlying writer.
Note that since this writes a single byte, no byte order conversions are used. It is included for completeness.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write signed 8 bit integers to a Write
:
use tokio_byteorder::{AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_i8(2).await.unwrap(); wtr.write_i8(-5).await.unwrap(); assert_eq!(wtr, b"\x02\xfb"); }
fn write_u16<'a, T: ByteOrder>(&'a mut self, n: u16) -> WriteU16<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes an unsigned 16 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write unsigned 16 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_u16::<BigEndian>(517).await.unwrap(); wtr.write_u16::<BigEndian>(768).await.unwrap(); assert_eq!(wtr, b"\x02\x05\x03\x00"); }
fn write_i16<'a, T: ByteOrder>(&'a mut self, n: i16) -> WriteI16<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a signed 16 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write signed 16 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_i16::<BigEndian>(193).await.unwrap(); wtr.write_i16::<BigEndian>(-132).await.unwrap(); assert_eq!(wtr, b"\x00\xc1\xff\x7c"); }
fn write_u24<'a, T: ByteOrder>(&'a mut self, n: u32) -> WriteU24<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes an unsigned 24 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write unsigned 24 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_u24::<BigEndian>(267).await.unwrap(); wtr.write_u24::<BigEndian>(120111).await.unwrap(); assert_eq!(wtr, b"\x00\x01\x0b\x01\xd5\x2f"); }
fn write_i24<'a, T: ByteOrder>(&'a mut self, n: i32) -> WriteI24<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a signed 24 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write signed 24 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_i24::<BigEndian>(-34253).await.unwrap(); wtr.write_i24::<BigEndian>(120111).await.unwrap(); assert_eq!(wtr, b"\xff\x7a\x33\x01\xd5\x2f"); }
fn write_u32<'a, T: ByteOrder>(&'a mut self, n: u32) -> WriteU32<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes an unsigned 32 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write unsigned 32 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_u32::<BigEndian>(267).await.unwrap(); wtr.write_u32::<BigEndian>(1205419366).await.unwrap(); assert_eq!(wtr, b"\x00\x00\x01\x0b\x47\xd9\x3d\x66"); }
fn write_i32<'a, T: ByteOrder>(&'a mut self, n: i32) -> WriteI32<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a signed 32 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write signed 32 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_i32::<BigEndian>(-34253).await.unwrap(); wtr.write_i32::<BigEndian>(1205419366).await.unwrap(); assert_eq!(wtr, b"\xff\xff\x7a\x33\x47\xd9\x3d\x66"); }
fn write_u48<'a, T: ByteOrder>(&'a mut self, n: u64) -> WriteU48<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes an unsigned 48 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write unsigned 48 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_u48::<BigEndian>(52360336390828).await.unwrap(); wtr.write_u48::<BigEndian>(541).await.unwrap(); assert_eq!(wtr, b"\x2f\x9f\x17\x40\x3a\xac\x00\x00\x00\x00\x02\x1d"); }
fn write_i48<'a, T: ByteOrder>(&'a mut self, n: i64) -> WriteI48<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a signed 48 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write signed 48 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_i48::<BigEndian>(-108363435763825).await.unwrap(); wtr.write_i48::<BigEndian>(77).await.unwrap(); assert_eq!(wtr, b"\x9d\x71\xab\xe7\x97\x8f\x00\x00\x00\x00\x00\x4d"); }
fn write_u64<'a, T: ByteOrder>(&'a mut self, n: u64) -> WriteU64<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes an unsigned 64 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write unsigned 64 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_u64::<BigEndian>(918733457491587).await.unwrap(); wtr.write_u64::<BigEndian>(143).await.unwrap(); assert_eq!(wtr, b"\x00\x03\x43\x95\x4d\x60\x86\x83\x00\x00\x00\x00\x00\x00\x00\x8f"); }
fn write_i64<'a, T: ByteOrder>(&'a mut self, n: i64) -> WriteI64<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a signed 64 bit integer to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write signed 64 bit big-endian integers to a Write
:
use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_i64::<BigEndian>(i64::min_value()).await.unwrap(); wtr.write_i64::<BigEndian>(i64::max_value()).await.unwrap(); assert_eq!(wtr, b"\x80\x00\x00\x00\x00\x00\x00\x00\x7f\xff\xff\xff\xff\xff\xff\xff"); }
fn write_u128<'a, T: ByteOrder>(
&'a mut self,
n: u128
) -> WriteU128<&'a mut Self> where
Self: Unpin,
&'a mut self,
n: u128
) -> WriteU128<&'a mut Self> where
Self: Unpin,
Writes an unsigned 128 bit integer to the underlying writer.
fn write_i128<'a, T: ByteOrder>(
&'a mut self,
n: i128
) -> WriteI128<&'a mut Self> where
Self: Unpin,
&'a mut self,
n: i128
) -> WriteI128<&'a mut Self> where
Self: Unpin,
Writes a signed 128 bit integer to the underlying writer.
fn write_f32<'a, T: ByteOrder>(&'a mut self, n: f32) -> WriteF32<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write a big-endian single-precision floating point number to a Write
:
use std::f32; use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_f32::<BigEndian>(f32::consts::PI).await.unwrap(); assert_eq!(wtr, b"\x40\x49\x0f\xdb"); }
fn write_f64<'a, T: ByteOrder>(&'a mut self, n: f64) -> WriteF64<&'a mut Self> where
Self: Unpin,
Self: Unpin,
Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer.
Errors
This method returns the same errors as Write::write_all
.
Examples
Write a big-endian double-precision floating point number to a Write
:
use std::f64; use tokio_byteorder::{BigEndian, AsyncWriteBytesExt}; #[tokio::main] async fn main() { let mut wtr = Vec::new(); wtr.write_f64::<BigEndian>(f64::consts::PI).await.unwrap(); assert_eq!(wtr, b"\x40\x09\x21\xfb\x54\x44\x2d\x18"); }
Implementors
impl<W: AsyncWrite + ?Sized> AsyncWriteBytesExt for W
[src]
All types that implement Write
get methods defined in WriteBytesExt
for free.