diff options
Diffstat (limited to 'include/bzipper.php')
-rw-r--r-- | include/bzipper.php | 52 |
1 files changed, 24 insertions, 28 deletions
diff --git a/include/bzipper.php b/include/bzipper.php index 645deca..af224b2 100644 --- a/include/bzipper.php +++ b/include/bzipper.php @@ -1,71 +1,67 @@ <?php add_heading("bzipper", "about"); ?> <section> - <p><em>bzipper</em> is a Rust crate for serialisation and deserialisation of binary streams.</p> + <p><a href="https://crates.io/crates/bzipper/">bzipper</a> is a binary (de)serialiser for the Rust language.</p> <br> - <p>See more at <code><a href="https://crates.io/crates/bzipper/">crates.io</a></code>.</p> -</section> - -<?php add_heading("Rationale", "rationale"); ?> - -<section> - <p>Contrary to <a href="https://crates.io/crates/serde/">Serde</a>/<a href="https://crates.io/crates/bincode/">Bincode</a>, the goal of this crate is to serialise data with a known size limit. Therefore, this crate may be more suited for networking or other cases where a fixed-sized buffer is needed.</p> + <p>Contrary to <a href="https://crates.io/crates/serde/">Serde</a>/<a href="https://crates.io/crates/bincode/">Bincode</a>, the goal of bzipper is to serialise with a known size constraint. Therefore, this crate may be more suited for networking or other cases where a fixed-sized buffer is needed.</p> + <br> + <p>Keep in mind that this project is still work-in-progress.</p> + <br> + <p>This crate is compatible with <code>no_std</code>.</p> </section> <?php add_heading("Data model", "dataModel"); ?> <section> - <p>Most primitive types serialise losslessly, with the exception being <code>usize</code> and <code>isize</code>. These serialise as <code>u16</code> and <code>u32</code>, respectively, for portability reasons.</p> + <p>Most primitive types serialise losslessly, with the exception being <code>usize</code> and <code>isize</code>. These serialise as <code>u32</code> and <code>i32</code>, respectively, for portability reasons.</p> <br> - <p>Unsized types, such as <code>str</code> and slices, are not supported. Instead, array should be used. For strings, the <code>FixedString</code> type is also provided.</p> + <p>Unsized types, such as <code>str</code> and slices, are not supported. Instead, arrays should be used. For strings, the <code>FixedString</code> type is also provided.</p> </section> <?php add_heading("Usage", "usage"); ?> <section> - <p>This crate revolves around the <code>Serialise</code> and <code>Deserialise</code> traits, both of which work around streams (more specifically, d-streams and s-streams).</p> + <p>This crate revolves around the <code>Serialise</code> and <code>Deserialise</code> traits, both of which are commonly used in conjunction with streams (more specifically, s-streams and d-streams).</p> <br> <p>Many core types come implemented with bzipper, including primitives as well as some standard library types such as <code>Option</code> and <code>Result</code>.</p> + <br> + <p>It is recommended in most cases to just derive these traits for custom types (enumerations and structures only). Here, each field is chained in declaration order:</p> + <br> + <p class="codeblock">use bzipper::{Deserialise, Serialise};<br><br>#[derive(Debug, Deserialise, PartialEq, Serialise)]<br>struct IoRegister {<br> addr: u32,<br> value: u16,<br>}<br><br>let mut buf: [u8; IoRegister::SERIALISED_SIZE] = Default::default();<br>IoRegister { addr: 0x04000000, value: 0x0402 }.serialise(&mut buf).unwrap();<br><br>assert_eq!(buf, [0x04, 0x00, 0x00, 0x00, 0x04, 0x02]);<br><br>assert_eq!(IoRegister::deserialise(&buf).unwrap(), IoRegister { addr: 0x04000000, value: 0x0402 });</p> </section> <?php add_heading("Serialisation", "serialisation"); ?> <section> - <p>To serialise an object implementing <code>Serialise</code>, simply allocate a so-called “s-stream” (short for <em>serialisation stream</em>) with the <code>Sstream</code> type:</p> - <br> - <p class="codeblock">let mut buf: [u8; 16] = Default::default();<br><br>let mut stream = bzipper::Sstream::new(&mut buf);</p> + <p>To serialise an object implementing <code>Serialise</code>, simply allocate a buffer for the serialisation. The required size of any given serialisation is specified by the <code>SERIALISED_SIZE</code> constant:</p> <br> - <p>The resulting stream is immutable in the sense that it cannot grow its buffer, altough it does keep track of the buffer's state.</p> + <p class="codeblock">use bzipper::Serialise;<br><br>let mut buf: [u8; char::SERIALISED_SIZE] = Default::default();<br>'Ж'.serialise(&mut buf).unwrap();<br><br>assert_eq!(buf, [0x00, 0x00, 0x04, 0x16]);</p> <br> - <p>A byte sequence can be added to our new stream by passing the stream to a call to the <code>serialise</code> method:</p> + <p>The only special requirement of the <code>serialise</code> method is that the provided byte slice has an element count of exactly <code>SERIALISED_SIZE</code>.</p> <br> - <p class="codeblock">use bzipper::Serialise;<br><br>let mut buf: [u8; 2] = Default::default();<br>let mut stream = bzipper::Sstream::new(&mut buf);<br><br>0x4554_u16.serialise(&mut stream).unwrap();</p> + <p>We can also use streams to <em>chain</em> multiple elements together:</p> <br> - <p>The ammount of bytes used by the serialiser (that is, the ammount of bytes written to the stream) is indicated by its return value (i.e. it has the type <code>Result<usize, Serialise::Error></code>).</p> - <br> - <p>Whilst the <em>maximum</em> ammount of bytes is specified by the <code>SERIALISE_LIMIT</code> constant, this can in cases be lower (for example with <code>None</code> variants which are always encoded as a single, null byte).</p> + <p class="codeblock">use bzipper::Serialise;<br><br>let mut buf: [u8; char::SERIALISED_SIZE * 5] = Default::default();<br>let mut stream = bzipper::Sstream::new(&mut buf);<br><br>stream.append(&'ل');<br>stream.append(&'ا');<br>stream.append(&'م');<br>stream.append(&'د');<br>stream.append(&'ا');<br><br>assert_eq!(buf, [0x00, 0x00, 0x06, 0x44, 0x00, 0x00, 0x06, 0x27, 0x00, 0x00, 0x06, 0x45, 0x00, 0x00, 0x06, 0x2F, 0x00, 0x00, 0x06, 0x27]);</p> <br> <p>When serialising primitives, the resulting byte stream is in big endian (a.k.a. network endian). It is recommended for implementors to adhere to this convention as well.</p> - <br> - <p>After serialisation, the s-stream records the new write-to position of the buffer. This allows for <em>chaining</em> of serialisations, which can prove useful when implementing the trait for custom types.</p> </section> <?php add_heading("Deserialisation", "deserialisation"); ?> <section> - <p>As with serialisation, deserialisation uses streams (just with the <code>Dstream</code> type; short for <em>deserialisation stream</em>):</p> + <p>Deserialisation works with an almost identical syntax to serialisation.</p> <br> - <p class="codeblock">let data = [0x45, 0x54];<br><br>let mut stream = bzipper::Dstream::new(&data);</p> + <p>To deserialise a buffer, simply call the <code>deserialise</code> method:</p> <br> - <p>Using these streams is also just as simple as with s-streams:</p> + <p class="codeblock">use bzipper::Deserialise;<br><br>let data = [0x45, 0x54];<br>assert_eq!(<u16>::deserialise(&data).unwrap(), 0x4554);</p> <br> - <p class="codeblock">use bzipper::Deserialise;<br><br>let data = [0x45, 0x54];<br>let mut stream = bzipper::Dstream::new(&data);<br><br>assert_eq!(u16::deserialise(&mut stream).unwrap(), 0x4554);</p> + <p>Just like with serialisations, the <code>Dstream</code> can be used to deserialise chained elements:</p> <br> - <p>When chaining serialisations, keep in mind that appropriate deserialisations should come in <strong>reverse order</strong> (streams function similarly to stacks in this sense).</p> + <p class="codeblock">use bzipper::Deserialise;<br><br>let data = [0x45, 0x54];<br>let stream = bzipper::Dstream::new(&data);<br><br>assert_eq!(stream.take::<u8>().unwrap(), 0x45);<br>assert_eq!(stream.take::<u8>().unwrap(), 0x54);</p> </section> <?php add_heading("Docs", "docs"); ?> <section> - <p>Documentation is written in-source. See <a href="https://docs.rs/pollex/latest/pollex/">Docs.rs</a> for a rendered instance.</p> + <p>Documentation is written in-source. See <a href="https://docs.rs/bzipper/latest/bzipper/">Docs.rs</a> for a rendered instance.</p> </section> |