Crate rsomeip_bytes

Crate rsomeip_bytes 

Source
Expand description

§rsomeip-bytes

A serialization crate for the SOME/IP on-wire format.

§Overview

This crate provides traits for the serialization and deserialization of data types according to the SOME/IP protocol.

It also provides implementation of said traits for all basic types supported by the protocol, as well as for some commonly used types from Rust’s standard library.

§Getting Started

  1. Add rsomeip-bytes as a dependency to your project.

    # Cargo.toml
    
    [dependencies]
    rsomeip-bytes = "0.1.0"
  2. Use the Serialize and Deserialize traits to write and read data.

    use rsomeip_bytes::{Serialize, Deserialize, BytesMut, LengthField};
    
    // Write data into a buffer using the `serialize` method.
    let mut buffer = BytesMut::new();
    1u8.serialize(&mut buffer).unwrap();
    
    // Use `serialize_len` method for dynamically sized types.
    let dyn_data = vec![1u8, 2];
    dyn_data.serialize_len(LengthField::U8, &mut buffer).unwrap();
    
    // Read data from a buffer using the `deserialize` method.
    let mut buffer = buffer.freeze();
    assert_eq!(u8::deserialize(&mut buffer), Ok(1u8));
    
    // Use `deserialize_len` for dynamically sized types.
    let dyn_data = Vec::<u8>::deserialize_len(LengthField::U8, &mut buffer).unwrap();
    assert_eq!(&dyn_data[..], &[1u8, 2]);
  3. Implement the traits for your custom types.

    use rsomeip_bytes::{
        Serialize, Deserialize, Buf, BufMut, SerializeError, DeserializeError,
        LengthField
    };
    
    struct Foo {
        a: u8,
        b: Vec<u8>,
    }
    
    impl Serialize for Foo {
        fn serialize(&self, buffer: &mut impl BufMut) -> Result<usize, SerializeError> {
            let mut size = 0;
            size += self.a.serialize(buffer)?;
            size += self.b.serialize_len(LengthField::U8, buffer)?;
            Ok(size)
        }
    
        fn size_hint(&self) -> usize {
            let mut size = 0;
            size += self.a.size_hint();
            size += 0u8.size_hint(); // Remember to include the size of the length field.
            size += self.b.size_hint();
            size
        }
    }
    
    impl Deserialize for Foo {
        type Output = Self;
    
        fn deserialize(buffer: &mut impl Buf) -> Result<Self, DeserializeError> {
            Ok(Foo{
                a: u8::deserialize(buffer)?,
                b: Vec::deserialize_len(LengthField::U8, buffer)?,
            })
        }
    }

§Motivation

The SOME/IP protocol specifies a list of supported data types and how to represent those types on wire.

This crate aims to enable Rust projects to represent their data types using this format.

§License

This project is licensed under either the Apache-2.0 License or MIT License, 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.

Macros§

deserialize_from
Deserializes a series of variables from the given buffer.
serialize_into
Serializes a series of variables into the given buffer.
size_hint
Returns the combined size hint of a series of variables.

Structs§

Bytes
A cheaply cloneable and sliceable chunk of contiguous memory.
BytesMut
A unique reference to a contiguous slice of memory.

Enums§

DeserializeError
Represents an error during the deserialization process.
LengthField
Size of the length field.
SerializeError
Represents an error during the serialization process.

Traits§

Buf
Read bytes from a buffer.
BufMut
A trait for values that provide sequential write access to bytes.
Deserialize
Deserialize data from a SOME/IP byte stream.
Serialize
Serialize data into a SOME/IP byte stream.
SerializeString
Serialize strings of various encodings into a SOME/IP byte stream.