BitIO

Trait BitIO 

Source
pub trait BitIO {
    // Required methods
    fn to_bit(&self) -> Vec<u8> ;
    fn from_bit(a: &[u8]) -> Self;
}
Expand description

将数据从原来的形式与字节形式之间进行转换

对基础的数字类型、Vec、String进行了实现

对于其他自定义类型,需要手动实现该trait,如:

use network_flow::io::BitIO;
use std::mem::size_of;
struct MyStruct {
    a : String,
    b : u32,
    c : Vec<String>
}
impl BitIO for MyStruct {
    fn to_bit(&self) -> Vec<u8> {
        let mut temp = self.a.to_bit();
        let mut res = temp.len().to_bit();
        res.append(&mut temp);
        res.append(&mut self.b.to_bit());
        res.append(&mut self.c.to_bit());
        res
    }
    fn from_bit(a : &[u8]) -> Self {
        let mut temp = size_of::<usize>();
        let len = usize::from_bit(a);
        let temp1 = String::from_bit(&a[temp..]);
        temp = temp + len;
        let temp2 = u32::from_bit(&a[temp..]);
        temp = temp + size_of::<u32>();
        let temp3 = Vec::<String>::from_bit(&a[temp..]);
        MyStruct { a: temp1, b: temp2, c: temp3 }
    }
}

使用实现了该trait的类型构建的图,可以进行自动的文件输入输出,保存当前图中的状态或者读取原来的图的状态。

Required Methods§

Source

fn to_bit(&self) -> Vec<u8>

转为字节形式

Source

fn from_bit(a: &[u8]) -> Self

从字节形式生成

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl BitIO for f32

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for f64

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for i8

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for i16

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for i32

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for i64

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for i128

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for isize

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for u8

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for u16

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for u32

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for u64

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for u128

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for usize

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl BitIO for String

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Source§

impl<T: BitIO> BitIO for Vec<T>

Source§

fn to_bit(&self) -> Vec<u8>

Source§

fn from_bit(a: &[u8]) -> Self

Implementors§