simple_stream/frame/mod.rs
1// Copyright 2015 Nathan Sizemore <nathanrsizemore@gmail.com>
2//
3// This Source Code Form is subject to the terms of the
4// Mozilla Public License, v. 2.0. If a copy of the MPL was not
5// distributed with this file, You can obtain one at
6// http://mozilla.org/MPL/2.0/.
7
8
9//! The `simple_stream::fame` module contains Trait definitions and built-in types for coupling
10//! with a stream type to provide a structred way to send and receive message through streams.
11//!
12//! # Frame and FrameBuilder
13//!
14//! A `Frame` is the unit that streams operate on. Frames are used to ensure a complete piece of
15//! information has been received and that complete pieces of information are sent without
16//! fragmentation. A `FrameBuilder` is used by the stream types to construct a `Frame` from a
17//! chunk of bytes.
18
19
20pub use self::simple::*;
21pub use self::websocket::*;
22pub use self::checksum32::*;
23
24mod simple;
25mod websocket;
26mod checksum32;
27
28/// The Frame trait allows for type construction/destruction to/from a chunk of bytes.
29pub trait Frame: Sync + Send {
30 /// Transforms this type into a `Vec<u8>` in order to send through a stream.
31 fn to_bytes(&self) -> Vec<u8>;
32 /// Returns the paylaod data section of this `Frame`
33 fn payload(&self) -> Vec<u8>;
34 /// Returns the total length of the frame as if `to_bytes().len()` was called.
35 fn len_as_vec(&self) -> usize;
36 /// Returns a `*mut ()` to the underlying frame in order to cast to/from a specific
37 /// type from the Trait Object returned from stream reads.
38 ///
39 /// It is up to the caller of this method to take care of the cleanup required of the specific
40 /// type the pointer was cast to (E.g. by calling `Box::from_raw(ptr)').
41 fn as_mut_raw_erased(&self) -> *mut ();
42}
43
44pub trait FrameBuilder {
45 /// Given a `&mut Vec<u8>`, this function should return a Frame Trait Object, if possible,
46 /// created from the bytes in `buf`. On success this method should remove all bytes that
47 /// were used during the creation of the returned frame, from `buf`.
48 fn from_bytes(buf: &mut Vec<u8>) -> Option<Box<dyn Frame>>;
49}