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 implement Packed for it:

use bytepack::Packed;

struct Foo {
    a: i16,
    b: f32,
    c: u8
}

impl Packed for Foo {
    fn switch_endianness(&mut self) {
        self.a.switch_endianness();
        self.b.switch_endianness();
        self.c.switch_endianness();
    }
}

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

Perform an in-place switch of the endianness. This might be a no-op in some cases.

Implementors