8 releases

0.3.0 May 20, 2023
0.2.0 May 13, 2023
0.1.5 Jan 3, 2023
0.1.3 Dec 18, 2022

#716 in #byte

Download history 4/week @ 2024-07-22 58/week @ 2024-07-29 3/week @ 2024-08-05 8/week @ 2024-08-12 20/week @ 2024-08-26 15/week @ 2024-09-02 16/week @ 2024-09-09 3/week @ 2024-09-16 42/week @ 2024-09-23 27/week @ 2024-09-30 1/week @ 2024-10-07 28/week @ 2024-10-14 2/week @ 2024-10-21 1/week @ 2024-10-28 27/week @ 2024-11-04

58 downloads per month
Used in 5 crates (via bytes-kman)

GPL-3.0 license

14KB
260 lines

Bytes

Crates.io

is used for serialize and deserialize

is useful because is more faster and smaller then json!
But not human readable!

Examples

use bytes_kman::prelude::*;

#[derive(Bytes)]
pub struct Package{
    id: u16,
    message: String
}


pub fn main(){
    let pak = Package{
        id: 21
        message: String::from("This is a message")
    };

    let mut bytes = pak.to_bytes();
    bytes.reverse();

    let other = Package::from_bytes(&mut bytes).unwrap();

    assert_eq!(pak, other);
}
use bytes_kman::prelude::*;

pub fn main(){
    let num = 32u8;
    let bytes = num::to_bytes();
    assert_eq!(bytes; vec![32]) 
}
use bytes_kman::prelude::*;

pub fn main(){
    let num = 32;
    let bytes = num::to_bytes();
    assert_eq!(bytes; vec![32, 0, 0, 0]) 
}
use bytes_kman::prelude::*;

pub fn main(){
    let string = "Hello World!".to_string();
    let bytes = num::to_bytes();
    //                    this is the string length 'H' 'e'  'l'  'l'  'o'  ' ' 'W' 'o'  'r'  'l'  'd' '!'
    //                     >>>>>>>>>>>>^<<<<<<<<<<   ^   ^    ^    ^    ^    ^   ^   ^    ^    ^    ^   ^
    assert_eq!(bytes; vec![12, 0, 0, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) 
}
use bytes_kman::prelude::*;

pub fn main(){
    let string = "Hello World!".to_string();

    let mut bytes = num::to_bytes();
    bytes.reverse(); // when reading is inversed

    let other = String::from_bytes(&mut bytes).unwrap() // if don't have enough bytes will result in an error!
    // bytes = []
    // bytes will be consumed!
    // other = "Hello World!"
    assert_eq!(string, other) 
}

Rust toolchain: stable 1.65.0

Dependencies

~1.5MB
~37K SLoC