Trait bytepack::Packed
[−]
[src]
pub trait Packed { fn switch_endianness(&mut self); }
This trait both identifies a type which holds his data packed together in memory and a type
which offers a switch_endianness
method. This trait is voluntarily not implemented for
isize
and usize
because their size can vary from one system to another.
Example
If you would like to read and write one of your struct using bytepack
, you can derive
Packed
for it:
extern crate bytepack; #[macro_use] extern crate bytepack_derive; use std::fs::File; use bytepack::{LEUnpacker, Packed}; #[derive(Packed)] struct Vector { x: f32, y: f32, z: f32, } #[derive(Packed)] struct RGB(u8,u8,u8); fn main() { let mut file = File::open("test").unwrap(); let vector : Vector = file.unpack().unwrap(); let rgb : RGB = file.unpack().unwrap(); }
Please note that also specifying #[repr(packed)]
might make sense if you want to get rid of
the padding inside your structure.
Packed
can only be derived for strutures only composed of types implementing Packed
themselves. If you which to circumvent this restriction you can implement Packed
yourselve,
however you need to make sure your struct is indeed "packed" and that reading and writing it as
one continuous memory zone makes sense. For example the following structures are not "packed"
because they all hold a reference to their data.
struct NotPacked1 { name: String } struct NotPacked2 { numbers: Vec<f32> } struct NotPacked3 { count: Rc<u64> }
Required Methods
fn switch_endianness(&mut self)
Perform an in-place switch of the endianness. This might be a no-op in some cases.
Implementors
impl Packed for bool
impl Packed for u8
impl Packed for i8
impl Packed for u16
impl Packed for i16
impl Packed for u32
impl Packed for i32
impl Packed for u64
impl Packed for i64
impl Packed for f32
impl Packed for f64
impl<T> Packed for [T; 1] where T: Packed
impl<T> Packed for [T; 2] where T: Packed
impl<T> Packed for [T; 3] where T: Packed
impl<T> Packed for [T; 4] where T: Packed
impl<T> Packed for [T; 5] where T: Packed