Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust docs for protocols::v2::codec-sv2 #1040

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
91aed38
Error variant cmts
rrybarczyk Jul 5, 2024
fff2d8e
Alphabetize error variants
rrybarczyk Jul 5, 2024
78c080f
Type + fn cmts
rrybarczyk Jul 5, 2024
0ad01ed
Top level error doc cmt
rrybarczyk Jul 5, 2024
377f2c2
Alphabetize Error impls match arms
rrybarczyk Jul 5, 2024
b662094
State enum + method doc cmts
rrybarczyk Jul 6, 2024
f2d813f
HandshakeRole enum + method doc cmts
rrybarczyk Jul 6, 2024
cb0d257
Top level lib cmts
rrybarczyk Jul 6, 2024
f21d667
Decoder types cmts
rrybarczyk Jul 6, 2024
848ded8
Decoder WithNoise+WithoutNoise cmts
rrybarczyk Jul 6, 2024
f7112b3
Top level decoder cmts
rrybarczyk Jul 6, 2024
5c2b3be
Merge branch 'dev' into 2024-07-rust-docs-protocols-codec-sv2
rrybarczyk Aug 9, 2024
9784333
StandardEitherFrame can be either encoded or decoded
rrybarczyk Aug 9, 2024
a48a6c6
StandardSv2Frame can be either encoded or decoded
rrybarczyk Aug 9, 2024
ed21bfa
Rm redundant cmt
rrybarczyk Aug 9, 2024
122064b
Merge branch 'dev' into 2024-07-rust-docs-protocols-codec-sv2
rrybarczyk Aug 27, 2024
5ec84bf
Merge branch 'main' into 2024-07-rust-docs-protocols-codec-sv2
rrybarczyk Sep 2, 2024
98fb6c1
Merge branch 'main' into 2024-07-rust-docs-protocols-codec-sv2
rrybarczyk Sep 10, 2024
f96c834
Reason for crate
rrybarczyk Sep 10, 2024
08a98bf
NoiseEncoder struct cmts
rrybarczyk Sep 10, 2024
1956a6d
NoiseEncoder w/o binary_sv2 doc cmts + decoder consistency
rrybarczyk Sep 10, 2024
af463fc
Encoder doc cmts + decoder consistency
rrybarczyk Sep 10, 2024
381b36c
Encoder Item type doc cmts
rrybarczyk Sep 10, 2024
76f3295
Encoder Buffer and Slice type doc cmts
rrybarczyk Sep 10, 2024
25e159d
Top mod encoder doc cmts + decoder consistency
rrybarczyk Sep 10, 2024
955e913
Top mod doc clean up
rrybarczyk Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 110 additions & 2 deletions protocols/v2/codec-sv2/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
//! # Stratum V2 Codec Decoder
//!
//! This module provides functionality for decoding Stratum V2 messages, including support for
//! the Noise protocol for secure communication.
//!
//! ## Features
//!
//! * **Standard Decoder**: Decodes Stratum V2 frames without encryption.
//! * **Noise Decoder**: Decodes Stratum V2 frames with Noise protocol encryption.
//!
//! ## Types
//!
//! * `WithoutNoise`: Decoder for Sv2 frames without Noise protocol support.
//! * `WithNoise`: Decoder for Sv2 frames with Noise protocol support (requires the `noise_sv2`
//! feature).
//! * `StandardEitherFrame`: Represents an encoded or decoded frame that could be either a regular
//! or Noise-protected frame.
//! * `StandardSv2Frame`: Represents an encoded or decoded Stratum V2 frame.
//! * `StandardNoiseDecoder`: Decoder for Stratum V2 frames with Noise protocol support.
//! * `StandardDecoder`: Decoder for Stratum V2 frames without Noise protocol support.
//!
//! ## Usage
//!
//! This module is designed to be used to decode incoming Sv2 messages, with optional Noise
//! protocol encryption support for secure communication.
//!
//! ## Example
//!
//! ```ignore
//! use codec_sv2::decoder::{StandardDecoder, StandardNoiseDecoder};
//!
//! // Create a standard decoder
//! let mut decoder: StandardDecoder<MyFrameType> = StandardDecoder::new();
//!
//! // Create a noise decoder (requires the `noise_sv2` feature)
//! #[cfg(feature = "noise_sv2")]
//! let mut noise_decoder: StandardNoiseDecoder<MyFrameType> = StandardNoiseDecoder::new();
//! ```
//!

Comment on lines +1 to +40
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this header is not going to be displayed in Rust Docs, because the decoder module is not exposed

overall I'd recommend to regularly do cargo doc --all-features --open to render the docs so you can have a good idea of what the end-user is going to see

Copy link
Collaborator

@Fi3 Fi3 Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we can change how we export thing in order to include it without changing the API

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry @Fi3 I don't fully understand, can you elaborate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add the --document-private-items to expose documentation for private modules like:

cargo doc --document-private-items --all-features --open

I left a comment here with some thoughts.

Essentially, I am not sure if you can publish the private crate documentation to crates.io, or if we would want to do that. BUT, we can use it to guide us and view our docs on private modules while we develop the documentation.

Either way, my example here sucks 😝.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep or we can make the module public in this case: we make codec_sv2::decoder public and in the module we make private all the things that are not exported by lib.rs so we don't change the api.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we should make these modules public.

#[cfg(feature = "noise_sv2")]
use binary_sv2::Deserialize;
#[cfg(feature = "noise_sv2")]
Expand Down Expand Up @@ -34,22 +74,51 @@ use crate::Error::MissingBytes;
#[cfg(feature = "noise_sv2")]
use crate::State;

#[cfg(feature = "noise_sv2")]
pub type StandardNoiseDecoder<T> = WithNoise<Buffer, T>;
/// An encoded or decoded frame that could either be a regular or Noise-protected frame.
pub type StandardEitherFrame<T> = Frame<T, <Buffer as IsBuffer>::Slice>;

/// An encoded or decoded Sv2 frame.
pub type StandardSv2Frame<T> = Sv2Frame<T, <Buffer as IsBuffer>::Slice>;

/// Standard decoder with Noise protocol support.
#[cfg(feature = "noise_sv2")]
pub type StandardNoiseDecoder<T> = WithNoise<Buffer, T>;

/// Standard Sv2 decoder without Noise protocol support.
pub type StandardDecoder<T> = WithoutNoise<Buffer, T>;

/// Decoder for Sv2 frames with Noise protocol support.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we can add an example of how to create and use a codec. If is possible to do it vai a doc test would be fantastic.

#[cfg(feature = "noise_sv2")]
pub struct WithNoise<B: IsBuffer, T: Serialize + binary_sv2::GetSize> {
/// Used to maintain type information for the generic parameter `T`, which represents the type
/// of frames being decoded.
///
/// `T` refers to a type that implements the necessary traits for serialization
/// (`binary_sv2::Serialize`), deserialization (`binary_sv2::Deserialize`), and size
/// calculation (`binary_sv2::GetSize`).
Comment on lines +93 to +98
Copy link
Collaborator

@plebhash plebhash Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private fields are never exposed to reader eyes

it's good to document this for maintainer eyes, but we should use Non Doc comments (// inlines and /* ... */ blocks), as proposed in #970 (comment)

frame: PhantomData<T>,

/// Number of missing bytes needed to complete the Noise header or payload.
///
/// Keeps track of how many more bytes are required to fully decode the current Noise frame.
Comment on lines +100 to +103
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

missing_noise_b: usize,

/// Buffer for holding encrypted Noise data.
///
/// Stores the incoming encrypted data until it is ready to be decrypted and processed.
Comment on lines +105 to +108
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

noise_buffer: B,

/// Buffer for holding decrypted Sv2 data.
///
/// Stores the decrypted data from the Noise protocol until it is ready to be processed and
/// concerted into a Sv2 frame.
Comment on lines +110 to +114
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

sv2_buffer: B,
}

#[cfg(feature = "noise_sv2")]
impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> WithNoise<B, T> {
/// Attempts to decode the next frame, returning either a frame or an error indicating how many
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also return other kind of error. So it return either a frame, an error, or an error with missing bytes. I did it like that but I think that maybe here having and enum into the Ok and the error would have been a better interface. Not sure if make sense to change it now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here an example would be nice. I just stop writing this but I think that when is possible we should add examples

/// bytes are missing.
#[inline]
pub fn next_frame(&mut self, state: &mut State) -> Result<Frame<T, B::Slice>> {
match state {
Expand Down Expand Up @@ -96,6 +165,11 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
}
}

/// Decodes a Noise-encrypted frame.
///
/// Handles the decryption of Noise-encrypted frames, including both the header and the
/// payload. It processes the frame in chunks if necessary, ensuring that all encrypted data is
/// properly decrypted and converted into a usable frame.
#[inline]
fn decode_noise_frame(&mut self, noise_codec: &mut NoiseCodec) -> Result<Frame<T, B::Slice>> {
match (
Expand Down Expand Up @@ -145,6 +219,11 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
}
}

/// Processes and decodes frames during the handshake phase of the Noise protocol.
///
/// Used while the codec is in the handshake phase of the Noise protocol. It decodes a received
/// handshake frame from the `noise_buffer`, converting it into a `HandShakeFrame`, and then
/// encapsulates it a `Frame`, marking it as ready for further processing by the codec.
fn while_handshaking(&mut self) -> Frame<T, B::Slice> {
let src = self.noise_buffer.get_data_owned().as_mut().to_vec();

Expand All @@ -154,17 +233,24 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
frame.into()
}

/// Provides a writable buffer for incoming data.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I would add that the buffer have the right number of bytes needed by the codec to go to the next step. I would also add an example of how to use the returned buffer.

#[inline]
pub fn writable(&mut self) -> &mut [u8] {
self.noise_buffer.get_writable(self.missing_noise_b)
}

/// Determines whether the decoder's internal buffers can be safely dropped.
pub fn droppable(&self) -> bool {
self.noise_buffer.is_droppable() && self.sv2_buffer.is_droppable()
}
}

#[cfg(feature = "noise_sv2")]
impl<T: Serialize + binary_sv2::GetSize> WithNoise<Buffer, T> {
/// Crates a new `WithNoise` decoder with default buffer sizes.
///
/// Initializes the decoder with default buffer sizes and sets the number of missing bytes to
/// 0.
pub fn new() -> Self {
Self {
frame: PhantomData,
Expand All @@ -182,14 +268,31 @@ impl<T: Serialize + binary_sv2::GetSize> Default for WithNoise<Buffer, T> {
}
}

/// Decoder for Sv2 frames without Noise protocol support.
#[derive(Debug)]
pub struct WithoutNoise<B: IsBuffer, T: Serialize + binary_sv2::GetSize> {
/// Marker for the type of frame being decoded.
///
/// Used to maintain type information for the generic parameter `T` which represents the type
/// of frames being decoded. `T` refers to a type that implements the necessary traits for
/// serialization (`binary_sv2::Serialize`), deserialization (`binary_sv2::Deserialize`), and
/// size calculation (`binary_sv2::GetSize`).
frame: PhantomData<T>,

/// Number of missing bytes needed to complete the frame.
///
/// Keeps track of how many more bytes are required to fully decode the current frame.
missing_b: usize,

/// Buffer for holding data to be decoded.
///
/// Stores incoming data until it is ready to be processed and converted into a Sv2 frame.
buffer: B,
}

impl<T: Serialize + binary_sv2::GetSize, B: IsBuffer> WithoutNoise<B, T> {
/// Attempts to decode the next frame, returning either a frame or an error indicating how many
/// bytes are missing.
#[inline]
pub fn next_frame(&mut self) -> Result<Sv2Frame<T, B::Slice>> {
let len = self.buffer.len();
Expand All @@ -210,12 +313,17 @@ impl<T: Serialize + binary_sv2::GetSize, B: IsBuffer> WithoutNoise<B, T> {
}
}

/// Provides a writable buffer for incoming data.
pub fn writable(&mut self) -> &mut [u8] {
self.buffer.get_writable(self.missing_b)
}
}

impl<T: Serialize + binary_sv2::GetSize> WithoutNoise<Buffer, T> {
/// Creates a new `WithoutNoise` with a buffer of default size.
///
/// Initializes the decoder with a default buffer size and sets the number of missing bytes to
/// the size of the header.
pub fn new() -> Self {
Self {
frame: PhantomData,
Expand Down
100 changes: 100 additions & 0 deletions protocols/v2/codec-sv2/src/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
//! # Stratum V2 Codec Encoder
//!
//! This module provides functionality for encoding Stratum V2 messages, including support for the
//! Noise protocol for secure communication.
//!
//! ## Features
//!
//! Two primary encoders are defined:
//! * **Standard Encoder**:
//! * **Noise Encoder**:
//!
//! ## Types
//!
//! * `Encoder`: Encoder for Sv2 frames with Noise protocol support.
//! * `NoiseEncoder`: Encoder for Sv2 frames without Noise protocol support (requires the
//! `noise_sv2` feature).
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! * `Encoder`: Encoder for Sv2 frames with Noise protocol support.
//! * `NoiseEncoder`: Encoder for Sv2 frames without Noise protocol support (requires the
//! `noise_sv2` feature).
//! * `Encoder`: Encoder for Sv2 frames without Noise protocol support (requires the
//! `noise_sv2` feature).
//! * `NoiseEncoder`: Encoder for Sv2 frames with Noise protocol support.

This should be the other way around.

//! * `Item<T>`: Represents an SV2 frame that is to be encoded and optionally encrypted using the
//! Noise protocol.
//! * `Buffer`: Holds intermediate data during encoding, which can be either a pool-allocated
//! buffer or a system memory buffer depending on the feature flags.
//! * `Slice`: Stores the serialized SV2 frame data before transmission.
//!
//! ## Usage
//!
//! This module is designed to be used to encode outgoing Sv2 messages, with optional Noise
//! protocol encryption support for secure communication.
//!
//! ## Example
//!
//! ```ignore
//! use codec_sv2::encoder::{Encoder, NoiseEncoder};
//! use framing_sv2::framing::Sv2Frame;
//!
//! // Create a standard encoder for unencrypted SV2 frames
//! let mut encoder: Encoder<MyFrameType> = Encoder::new();
//!
//! // Encode a frame into a byte slice
//! let frame = Sv2Frame::new( /* frame details */ );
//! let encoded_data = encoder.encode(frame).unwrap();
//!
//! // Create a Noise encoder for encrypted SV2 frames (requires the `noise_sv2` feature)
//! #[cfg(feature = "noise_sv2")]
//! let mut noise_encoder: NoiseEncoder<MyFrameType> = NoiseEncoder::new();
//! ```
Comment on lines +1 to +44
Copy link
Collaborator

@plebhash plebhash Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this header is not going to be displayed in Rust Docs, because the encoder module is not exposed

overall I'd recommend to regularly do cargo doc --all-features --open to render the docs so you can have a good idea of what the end-user is going to see

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should still impose our documentation standards on private modules, regardless of whether it will be generated. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no if a module is private we don't have to document it for user but for maintainers so is a different thing. In that case the module should not be private. A rule of thumb is, if you thing that you need to document a private module for the user maybe the module should not be private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we should not document private modules. If there is some private module that is very vague, and we feel strongly about adding a comment, maybe we can use regular comment and not ///?

Copy link
Collaborator

@plebhash plebhash Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @rrybarczyk in the sense that it's important that we don't fully ignore parts of the code just because they are not exposed. Afterall, one of the main goals of this collective effort is for SRI contributors to refine their understanding (and ultimately, their ownership) over these crates.

I see @rrybarczyk also suggested cargo doc --document-private-items --all-features --open on the other comment thread. I feel this could be dangerous because it could create a blindspot because we're not seeing what the user sees.

But I would follow @Fi3 advice and simply make encoder and decoder modules public:

--- a/protocols/v2/codec-sv2/src/lib.rs
+++ b/protocols/v2/codec-sv2/src/lib.rs
@@ -59,8 +59,8 @@ extern crate alloc;
 #[cfg(feature = "noise_sv2")]
 use alloc::boxed::Box;
 
-mod decoder;
-mod encoder;
+pub mod decoder;
+pub mod encoder;

But for everything that will not be public and we still want to document for maintainer eyes, we should do Non Doc comments, which are essentially // inlines and /* ... */ blocks

Copy link
Collaborator

@plebhash plebhash Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I formalized my proposal around Non Docs comments on the Discussion for Rust Docs Standards here #970 (comment)

Comment on lines +28 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure if writing this is really helpful. It could be misleading for someone referencing it, as they might assume these are public-facing APIs. I believe we should include code snippets only for public-facing modules, rather than for all modules.

Copy link
Collaborator

@plebhash plebhash Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make these modules public

but with regards to the broader discussion around this topic, let's try to reach consensus here #970 (comment)

use alloc::vec::Vec;
use binary_sv2::{GetSize, Serialize};
#[allow(unused_imports)]
Expand Down Expand Up @@ -25,28 +69,61 @@ use buffer_sv2::{Buffer as IsBuffer, BufferFromSystemMemory as Buffer};
#[cfg(feature = "with_buffer_pool")]
use buffer_sv2::{Buffer as IsBuffer, BufferFromSystemMemory, BufferPool};

/// The buffer type for holding intermediate data during encoding.
///
/// When the `with_buffer_pool` feature is enabled, `Buffer` is a pool-allocated buffer type
/// (`BufferPool`), which allows for more efficient memory management. Otherwise, it defaults to
/// `BufferFromSystemMemory`.
Comment on lines +74 to +76
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should reproduce this comment on decoder.rs because it also has a type alias for Buffer

///
/// `Buffer` is used for storing both serialized Sv2 frames and encrypted Noise data.
#[cfg(feature = "noise_sv2")]
#[cfg(feature = "with_buffer_pool")]
type Buffer = BufferPool<BufferFromSystemMemory>;

#[cfg(not(feature = "with_buffer_pool"))]
type Slice = Vec<u8>;

/// Holds the frame's serialized bytes before transmission.
#[cfg(feature = "with_buffer_pool")]
type Slice = buffer_sv2::Slice;

/// Encoder for Sv2 frames with Noise protocol support.
#[cfg(feature = "noise_sv2")]
pub struct NoiseEncoder<T: Serialize + binary_sv2::GetSize> {
/// Buffer for holding encrypted Noise data.
///
/// Stores the encrypted data from the Sv2 frame after it has been processed by the Noise
/// protocol and is ready to be transmitted.
noise_buffer: Buffer,

/// Buffer for holding serialized Sv2 data.
///
/// Stores the data to be encrypted by the Noise protocol after being encoded into a Sv2 frame.
sv2_buffer: Buffer,

/// Used to maintain type information for the generic parameter `T`, which represents the type
/// of frames being encoded.
///
/// `T` refers to a type that implements the necessary traits for serialization
/// (`binary_sv2::Serialize`) and size calculation (`binary_sv2::GetSize`).
frame: PhantomData<T>,
}

/// A Sv2 frame to be encoded and optionally encrypted using the Noise protocol.
///
/// `Item` is primarily used in the context of encoding frames before transmission. It is an alias
/// to a `Frame` that contains a payload of type `T` and uses a `Slice` as the underlying buffer.
/// This type is used during the encoding process to encapsulate the data being processed.
#[cfg(feature = "noise_sv2")]
type Item<T> = Frame<T, Slice>;

#[cfg(feature = "noise_sv2")]
impl<T: Serialize + GetSize> NoiseEncoder<T> {
/// Encodes a Noise-encrypted frame.
///
/// Encodes the given `item` into a Sv2 frame and then encrypts the frame using the Noise
/// protocol. The `state` of the Noise codec determines whether the encoder is in the handshake
/// or transport phase. On success, an encrypted frame as a `Slice` is returned.
#[inline]
pub fn encode(&mut self, item: Item<T>, state: &mut State) -> Result<Slice> {
match state {
Expand Down Expand Up @@ -99,6 +176,12 @@ impl<T: Serialize + GetSize> NoiseEncoder<T> {
Ok(self.noise_buffer.get_data_owned())
}

/// Processes and encodes Sv2 frames during the handshake phase of the Noise protocol.
///
/// Used when the codec is in the handshake phase. It encodes the provided `item` into a
/// handshake frame and stores the payload in the `noise_buffer`. This is necessary to
/// establish the initial secure communication channel before transitioning to the transport
/// phase of the Noise protocol.
#[inline(never)]
fn while_handshaking(&mut self, item: Item<T>) -> Result<()> {
// ENCODE THE SV2 FRAME
Expand All @@ -114,13 +197,15 @@ impl<T: Serialize + GetSize> NoiseEncoder<T> {
Ok(())
}

/// Determines whether the encoder's internal buffers can be safely dropped.
pub fn droppable(&self) -> bool {
self.noise_buffer.is_droppable() && self.sv2_buffer.is_droppable()
}
}

#[cfg(feature = "noise_sv2")]
impl<T: Serialize + binary_sv2::GetSize> NoiseEncoder<T> {
/// Creates a new `NoiseEncoder` with default buffer sizes.
pub fn new() -> Self {
#[cfg(not(feature = "with_buffer_pool"))]
let size = 512;
Expand All @@ -141,13 +226,27 @@ impl<T: Serialize + GetSize> Default for NoiseEncoder<T> {
}
}

/// Encoder for Sv2 frames without Noise protocol support.
#[derive(Debug)]
pub struct Encoder<T> {
/// Buffer for holding data to be encoded.
///
/// Stores data to be processed and converted into an Sv2 frame before it is transmitted.
buffer: Vec<u8>,

/// Marker for the type of frame being encoded.
///
/// Used to maintain type information for the generic parameter `T`, which represents the type
/// of frames being encoded. `T` refers to a type that implements the necessary traits for
/// serialization (`binary_sv2::Serialize`) and size calculation (`binary_sv2::GetSize`).
frame: PhantomData<T>,
}

impl<T: Serialize + GetSize> Encoder<T> {
/// Encodes the provided `item` into a Sv2 frame, storing the result in the internal buffer.
///
/// The frame of type `T` is serialized and placed in into the buffer, preparing it for
/// transmission.
pub fn encode(
&mut self,
item: Sv2Frame<T, Slice>,
Expand All @@ -161,6 +260,7 @@ impl<T: Serialize + GetSize> Encoder<T> {
Ok(&self.buffer[..])
}

/// Creates a new `Encoder` with a buffer of default size.
pub fn new() -> Self {
Self {
buffer: Vec::with_capacity(512),
Expand Down
Loading
Loading