Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Jan 16, 2023
1 parent 6319bcc commit 466a688
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions networking/firmware_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate alloc;

mod clientbound;
mod sansio;
mod serverbound;

pub use clientbound::*;
Expand Down
5 changes: 5 additions & 0 deletions networking/firmware_protocol/src/sansio/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! A WIP sans-io implementation of the firmware protocol.
//!
//! sans-io means that it performs no io and can be used in async or non async code.
mod serialization;
25 changes: 25 additions & 0 deletions networking/firmware_protocol/src/sansio/serialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// A simpler alternative to [`SerializeExact`], which will serialize without needing an
/// exact buffer size.
pub trait Serialize {
type Error;
/// Serializes into `buf`, returning the number of bytes written, or an error.
/// Note that this must not return `Ok` if only part of `self` was actually serialized.
fn serialize(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
}

/// Serializes directly into a buffer, such as directly into the tcp/udp buffers with no
/// the exact size known.
pub trait SerializeExact {
type Error;
/// Serializes the packet into the provided buffer. Implementations may choose to steal
/// `self`, or perform a copy, or they may have already serialized into bytes preemptively
/// (such as with flatbuffers).
///
/// # Panics
/// May panic if `f` returns an Ok variant with a buffer that is not the exact size as
/// `f`'s argument.
fn serialize_exact<'a, 'b>(
&'a mut self,
f: impl FnOnce(usize) -> nb::Result<&'b mut [u8], Self::Error>, // TODO: This might not work
) -> nb::Result<(), Self::Error>;
}

0 comments on commit 466a688

Please sign in to comment.