4 releases (stable)

2.1.0 Mar 4, 2019
2.0.0 Feb 28, 2019
1.0.0 Oct 10, 2018
0.1.0 Oct 10, 2018

#5 in Encoding

Download history 5/week @ 2019-04-03 10/week @ 2019-04-10 7/week @ 2019-04-17 37/week @ 2019-04-24 1/week @ 2019-05-01 5/week @ 2019-05-08 16/week @ 2019-05-15 9/week @ 2019-05-22 12/week @ 2019-05-29 6/week @ 2019-06-05 16/week @ 2019-06-12 13/week @ 2019-06-19 23/week @ 2019-06-26 18/week @ 2019-07-03 46/week @ 2019-07-10

96 downloads per month
Used in 2 crates

Apache-2.0

8KB

Build Status Rust Version 1.28+ Crate Docs

Codicon is a crate containing two simple traits (Encoder and Decoder) that express relationships between a native Rust type and its possible encodings.

We often need to express that a type can be encoded or decoded. We also need a way to express the type of the encoding or decoding as well as parameters that may be used for that encoding or decoding. This tiny crate solves this problem.

Install

Run this command:

$ cargo add codicon

Examples

Let's say we want u8 to be able to be encoded in a (made up) format Foo which simply writes the byte without modification. We can express this encoding as follows:

use std::io::{Result, Write};
use std::slice;

use codicon::Encoder;

struct Foo;

impl Encoder<Foo> for u8 {
    fn encode<W: Write>(&self, writer: &mut W, params: Foo) -> Result<()> {
        writer.write_all(slice::from_ref(self))?;
        Ok(())
    }
}

let mut buf = [0u8; 1];
7u8.encode(&mut buf.as_mut(), Foo).unwrap();
assert_eq!(buf[0], 7u8);

Note that we used a unit struct because the Foo encoding doesn't take any options. But if you wanted to specify encoding options, you could just make a type with parameters.

Decoding works the same as encoding:

use std::io::{Result, Read};
use std::slice;

use codicon::Decoder;

struct Foo;

impl Decoder<Foo> for u8 {
    fn decode<R: Read>(reader: &mut R, params: Foo) -> Result<Self> {
        let mut byte = 0u8;
        reader.read_exact(slice::from_mut(&mut byte))?;
        Ok(byte)
    }
}

let buf = [7u8; 1];
assert_eq!(u8::decode(&mut buf.as_ref(), Foo).unwrap(), 7u8);

No runtime deps