pub struct DMSCFrameBuilder { /* private fields */ }Expand description
Frame builder for creating protocol frames with automatic sequence numbering.
The DMSCFrameBuilder provides a convenient interface for constructing DMSC frames while automatically managing sequence numbers. Rather than manually tracking and incrementing sequence numbers for each frame, the builder maintains an internal counter that is automatically incremented after each frame construction. This ensures proper sequence numbering without the risk of human error.
§Builder Pattern Advantages
Using the frame builder provides several benefits over direct frame construction:
- Automatic Sequencing: No need to manually track and increment sequence numbers
- Type Safety: Compile-time checking of frame type construction
- Convenience Methods: Domain-specific constructors for each frame type
- State Management: Builder maintains state across frame constructions
§Sequence Number Management
The builder maintains an internal sequence counter that is automatically incremented
after each frame construction. The counter uses wrapping arithmetic (modulo 2^32)
to handle overflow gracefully. You can query or set the current sequence number
using next_sequence() and set_sequence() methods.
§Python Bindings
When compiled with the pyo3 feature, this struct provides Python bindings:
from dmsc import DMSCFrameBuilder
# Create builder for convenient frame construction
builder = DMSCFrameBuilder.new()
# Build frames without manually tracking sequence numbers
control_frame = builder.build_control_frame(b"\x01\x02\x03")
data_frame = builder.build_data_frame(b"Hello, World!")
auth_frame = builder.build_auth_frame(b"token=abc123")
# Check current sequence number
print(f"Next sequence: {builder.next_sequence()}")
# Reset sequence for new session
builder.set_sequence(0)§Examples
Building multiple frames with automatic sequencing:
use dmsc::protocol::frames::DMSCFrameBuilder;
let mut builder = DMSCFrameBuilder::new();
// Build a series of data frames
let frame1 = builder.build_data_frame(b"Message 1".to_vec())
.expect("Failed to build frame");
let frame2 = builder.build_data_frame(b"Message 2".to_vec())
.expect("Failed to build frame");
let frame3 = builder.build_data_frame(b"Message 3".to_vec())
.expect("Failed to build frame");
assert_eq!(frame1.sequence_number(), 0);
assert_eq!(frame2.sequence_number(), 1);
assert_eq!(frame3.sequence_number(), 2);
// Current sequence is now 3
assert_eq!(builder.next_sequence(), 3);Building different frame types:
use dmsc::protocol::frames::DMSCFrameBuilder;
let mut builder = DMSCFrameBuilder::new();
// Control frame
let control = builder.build_control_frame(vec![0x01, 0x00, 0x01])
.expect("Failed to build control frame");
// Authentication frame
let auth = builder.build_auth_frame(b"credentials=secret".to_vec())
.expect("Failed to build auth frame");
// Keep-alive frame
let keepalive = builder.build_keepalive_frame()
.expect("Failed to build keepalive frame");
// Error frame
let error = builder.build_error_frame(0x0401, "Timeout".to_string())
.expect("Failed to build error frame");Managing sequence numbers:
use dmsc::protocol::frames::DMSCFrameBuilder;
let mut builder = DMSCFrameBuilder::new();
// Build some frames
let _ = builder.build_data_frame(b"Frame 0".to_vec()).unwrap();
let _ = builder.build_data_frame(b"Frame 1".to_vec()).unwrap();
let _ = builder.build_data_frame(b"Frame 2".to_vec()).unwrap();
// Check current sequence
assert_eq!(builder.next_sequence(), 3);
// Set specific sequence for resend or new session
builder.set_sequence(100);
let next = builder.build_data_frame(b"Frame 100".to_vec()).unwrap();
assert_eq!(next.sequence_number(), 100);
assert_eq!(builder.next_sequence(), 101);§Thread Safety
This struct is not thread-safe. Multiple threads should not concurrently access the same builder instance without external synchronization. For concurrent frame building, either use separate builder instances per thread or wrap access with a Mutex or RwLock.
§Performance Characteristics
- Frame construction is O(1) for fixed-size headers
- Payload copying is O(n) where n is payload size
- Sequence number operations are O(1)
- Minimal heap allocation for small payloads
Implementations§
Source§impl DMSCFrameBuilder
impl DMSCFrameBuilder
pub fn new() -> Self
pub fn build_control_frame( &mut self, control_data: Vec<u8>, ) -> DMSCResult<DMSCFrame>
pub fn build_data_frame(&mut self, data: Vec<u8>) -> DMSCResult<DMSCFrame>
pub fn build_auth_frame(&mut self, auth_data: Vec<u8>) -> DMSCResult<DMSCFrame>
pub fn build_keepalive_frame(&mut self) -> DMSCResult<DMSCFrame>
pub fn build_error_frame( &mut self, error_code: u32, error_message: String, ) -> DMSCResult<DMSCFrame>
pub fn next_sequence(&self) -> u32
pub fn set_sequence(&mut self, sequence: u32)
Trait Implementations§
Source§impl<'py> IntoPyObject<'py> for DMSCFrameBuilder
impl<'py> IntoPyObject<'py> for DMSCFrameBuilder
Source§type Target = DMSCFrameBuilder
type Target = DMSCFrameBuilder
Source§type Output = Bound<'py, <DMSCFrameBuilder as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <DMSCFrameBuilder as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClass for DMSCFrameBuilder
impl PyClass for DMSCFrameBuilder
Source§impl PyClassImpl for DMSCFrameBuilder
impl PyClassImpl for DMSCFrameBuilder
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// - Minimal heap allocation for small payloads
const RAW_DOC: &'static CStr = /// - Minimal heap allocation for small payloads
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<DMSCFrameBuilder>
type ThreadChecker = SendablePyClass<DMSCFrameBuilder>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyTypeInfo for DMSCFrameBuilder
impl PyTypeInfo for DMSCFrameBuilder
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.impl DerefToPyAny for DMSCFrameBuilder
impl ExtractPyClassWithClone for DMSCFrameBuilder
Auto Trait Implementations§
impl Freeze for DMSCFrameBuilder
impl RefUnwindSafe for DMSCFrameBuilder
impl Send for DMSCFrameBuilder
impl Sync for DMSCFrameBuilder
impl Unpin for DMSCFrameBuilder
impl UnwindSafe for DMSCFrameBuilder
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read more