Code Monkey home page Code Monkey logo

persia-speedy's Introduction

Example

use std::borrow::Cow;
use persia_speedy::{Readable, Writable, Endianness};

#[derive(PartialEq, Debug, Readable, Writable)]
enum Enum {
    A,
    B,
    C,
}

#[derive(PartialEq, Debug, Readable, Writable)]
struct Struct< 'a > {
    number: u64,
    string: String,
    vector: Vec< u8 >,
    cow: Cow< 'a, [i64] >,
    float: f32,
    enumeration: Enum
}

fn main() {
    let original = Struct {
        number: 0x12345678ABCDEF00,
        string: "A totally pointless string".to_owned(),
        vector: vec![ 1, 2, 3 ],
        cow: Cow::Borrowed( &[ 4, 5, 6 ] ),
        float: 3.1415,
        enumeration: Enum::C
    };

    let endian = Endianness::LittleEndian;
    let bytes = original.write_to_vec_with_ctx( endian ).unwrap();
    let deserialized: Struct =
        Struct::read_from_buffer_with_ctx( endian, &bytes ).unwrap();

    assert_eq!( original, deserialized );
}

Field attributes

#[speedy(length = ...)]

Can be used on most standard containers to specify the field's length. Can refer to any of the previous fields.

For example:

use persia_speedy::{Readable, Writable};

#[derive(Readable, Writable)]
struct Struct {
    byte_count: u8,
    #[speedy(length = byte_count / 4)]
    data: Vec< u32 >
}

Before serializing you need to make sure that whatever is set as length is equal to the .len() of the field; if it's not then you will get an error when trying to serialize it.

Setting this attribute changes the serialization format as follows:

Type Serialized as
Vec<T> [T]
Cow<'a, [T]> [T]
String [u8]
Cow<'a, str> [u8]
HashMap<K, V> [K, V]
BTreeMap<K, V> [K, V]
HashSet<T> [T]
BTreeSet<T> [T]

#[speedy(length_type = ...)]

Can be used to specify the exact size of the implicit length field of a container as it is read or written.

Possible values:

  • u7 (same as u8, but restricted to 7 bits for u64_varint compatibility)
  • u8
  • u16
  • u32 (default)
  • u64_varint

#[speedy(skip)]

Skips a given field when reading and writing.

#[speedy(default_on_eof)]

If an EOF is encountered when reading this field its value will be set to the default value for its type and the EOF will be ignored.

#[speedy(constant_prefix = ...)]

Specifies a static string of bytes which will be written or has to be present when reading before a given field.

Enum attributes

#[speedy(tag_type = ...)]

Can be used to specify the exact size of the enum's tag as it is read or written.

Possible values:

  • u7 (same as u8, but restricted to 7 bits for u64_varint compatibility)
  • u8
  • u16
  • u32 (default)
  • u64_varint

#[speedy(peek_tag)]

An enum marked with this attribute will not consume its tag value when reading from a stream, nor will it write its own tag when writing.

Enum variant attributes

#[speedy(tag = ...)]

Specifies a preset tag value to be used for a given enum variant.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

persia-speedy's People

Contributors

koute avatar nobles5e avatar

Stargazers

liuhatry avatar ConleyLee avatar EnanaShinonome avatar Yulong Wang avatar

Forkers

org-mars

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.