[][src]Trait tobytes::ByteView

pub trait ByteView {
    pub fn byte_at(&self, index: usize) -> Option<u8>;
pub fn byte_size(&self) -> usize; }

The ByteView trait allows a type to provide a continues byte view of itself.

Example(s)

use tobytes::ByteView;
use tobytes::ToBytes;
use std::fmt::Pointer;
use std::marker::Sized;

struct Foo {
    field1: u8,
    field2: u16,
}

impl ByteView for Foo {
    fn byte_at(&self, index: usize) -> Option<u8> {
        if index < ByteView::byte_size(self) {
            match index {
                0 => self.field1.byte_at(index),
                1..=2 => self.field2.byte_at(index -1),
                _ => None
            }       
        }           
        else {
           None
        }   
    }

    fn byte_size(&self) -> usize {
        ByteView::byte_size(&self.field1) + ByteView::byte_size(&self.field2)
    }
}

let foo = Foo {field1: 0xFF, field2: 0xAABBu16.to_be()};
let bytes = foo.to_bytes().collect::<Vec<u8>>();

assert_eq!(3, foo.byte_size());
assert_eq!(vec![0xFF, 0xAA, 0xBB], bytes);

Required methods

pub fn byte_at(&self, index: usize) -> Option<u8>[src]

Get the byte at a specific index/location, if the index is out of bounce None should be returned.

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

Gets the size of the type when represented as ByteView. For indexes 0..bytes_size the ByteView needs to yield a valid byte Some(byte).

Loading content...

Implementations on Foreign Types

impl ByteView for u8[src]

impl ByteView for i8[src]

impl ByteView for u16[src]

impl ByteView for i16[src]

impl ByteView for u32[src]

impl ByteView for i32[src]

impl ByteView for u64[src]

impl ByteView for i64[src]

impl ByteView for u128[src]

impl ByteView for i128[src]

impl ByteView for f32[src]

impl ByteView for f64[src]

Loading content...

Implementors

Loading content...