Skip to main content

GenericLayerBuilder

Struct GenericLayerBuilder 

Source
pub struct GenericLayerBuilder { /* private fields */ }
Expand description

Builder for dynamically-defined custom protocol layers.

This builder is intended to be constructed once per protocol definition (e.g. in Python via the PyO3 bindings) and then used to produce byte buffers that conform to the protocol layout.

§Example

use std::sync::Arc;
use stackforge_core::layer::generic::builder::GenericLayerBuilder;
use stackforge_core::layer::generic::GenericFieldDesc;
use stackforge_core::layer::field::FieldType;

let fields = Arc::new(vec![
    GenericFieldDesc {
        name: "type_id".to_string(),
        offset: 0,
        size: 2,
        field_type: FieldType::U16,
        default_value: vec![0x00, 0x01],
    },
    GenericFieldDesc {
        name: "flags".to_string(),
        offset: 2,
        size: 1,
        field_type: FieldType::U8,
        default_value: vec![0x00],
    },
]);

let buf = GenericLayerBuilder::new(Arc::from("MyProto"), fields)
    .set_u16("type_id", 0x0042)
    .build();

assert_eq!(buf.len(), 3);
assert_eq!(buf[0], 0x00);
assert_eq!(buf[1], 0x42);
assert_eq!(buf[2], 0x00);

Implementations§

Source§

impl GenericLayerBuilder

Source

pub fn new(name: Arc<str>, field_descs: Arc<Vec<GenericFieldDesc>>) -> Self

Create a builder for the named protocol with the given field descriptors.

Source

pub fn set(self, name: &str, value: Vec<u8>) -> Self

Set a field by raw bytes.

If value is shorter than the field’s declared size it is zero-padded (on the right); if it is longer it is truncated.

Source

pub fn set_u8(self, name: &str, value: u8) -> Self

Set a U8 field by name.

Source

pub fn set_u16(self, name: &str, value: u16) -> Self

Set a U16 field by name (stored big-endian).

Source

pub fn set_u32(self, name: &str, value: u32) -> Self

Set a U32 field by name (stored big-endian).

Source

pub fn header_size(&self) -> usize

Total byte size of the protocol header (sum of all field sizes).

Source

pub fn build(&self) -> Vec<u8>

Build the layer into a byte buffer.

Fields not explicitly set fall back to their default_value. Each field value is copied into the buffer starting at field.offset, padded with zeros on the right or truncated if the value’s length does not match field.size.

Source

pub fn build_layer(&self) -> (GenericLayer, Vec<u8>)

Build the byte buffer and wrap it in a GenericLayer.

The returned layer’s LayerIndex covers [0..header_size] within the returned byte buffer.

Source

pub fn name(&self) -> &str

The protocol name.

Source

pub fn kind(&self) -> LayerKind

The layer kind (always LayerKind::Generic).

Trait Implementations§

Source§

impl Clone for GenericLayerBuilder

Source§

fn clone(&self) -> GenericLayerBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GenericLayerBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V