Struct RecordRef

Source
pub struct RecordRef<'a> { /* private fields */ }
Expand description

A wrapper around a non-owning immutable reference to a DBN record. This wrapper allows for mixing of record types and schemas and runtime record polymorphism.

It has the has() method for testing if the contained value is of a particular type, and the inner value can be downcasted to specific record types via the get() method.

use dbn::{MboMsg, RecordRef, TradeMsg};

let mbo = MboMsg::default();
let rec = RecordRef::from(&mbo);

// This isn't a trade
assert!(!rec.has::<TradeMsg>());
// It's an MBO record
assert!(rec.has::<MboMsg>());

// `get()` can be used in `if let` chains:
if let Some(_trade) = rec.get::<TradeMsg>() {
    panic!("Unexpected record type");
} else if let Some(mbo) = rec.get::<MboMsg>() {
    println!("{mbo:?}");
}

The common record header is directly accessible through the header() method.

Implementations§

Source§

impl<'a> RecordRef<'a>

Source

pub unsafe fn new(buffer: &'a [u8]) -> Self

Constructs a new reference to the DBN record in buffer.

§Safety

buffer should begin with a RecordHeader and contain a type implementing HasRType.

Source

pub unsafe fn unchecked_from_header(header: *const RecordHeader) -> Self

Constructs a new reference to the DBN record.

§Safety

header must point to a valid DBN record.

Source

pub fn has<T: HasRType>(&self) -> bool

Returns true if the object points to a record of type T.

Source

pub fn get<T: HasRType>(&self) -> Option<&'a T>

Returns a reference to the underlying record of type T or None if it points to another record type.

Note: for safety, this method calls has::<T>(). To avoid a duplicate check, use get_unchecked().

§Panics

This function will panic if the rtype indicates it’s of type T but the encoded length of the record is less than the size of T.

Source

pub fn as_enum(&self) -> Result<RecordRefEnum<'_>>

Returns a native Rust enum with a variant for each record type. This allows for pattern matching.

§Errors

This function returns a conversion error if the rtype does not correspond with any known DBN record type.

Source

pub unsafe fn get_unchecked<T: HasRType>(&self) -> &'a T

Returns a reference to the underlying record of type T without checking if this object references a record of type T.

For a safe alternative, see get().

§Safety

The caller needs to validate this object points to a T.

Trait Implementations§

Source§

impl<'a> AsRef<[u8]> for RecordRef<'a>

Source§

fn as_ref(&self) -> &'a [u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for RecordRef<'a>

Source§

fn clone(&self) -> RecordRef<'a>

Returns a copy 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 RecordRef<'_>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a, R> From<&'a R> for RecordRef<'a>
where R: HasRType,

Source§

fn from(rec: &'a R) -> Self

Constructs a new reference to a DBN record.

Source§

impl<'a> From<&'a RecordEnum> for RecordRef<'a>

Source§

fn from(rec_enum: &'a RecordEnum) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<RecordRefEnum<'a>> for RecordRef<'a>

Source§

fn from(rec_enum: RecordRefEnum<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> Record for RecordRef<'a>

Source§

fn header(&self) -> &'a RecordHeader

Returns a reference to the RecordHeader that comes at the beginning of all record types.
Source§

fn raw_index_ts(&self) -> u64

Returns the raw primary timestamp for the record. Read more
Source§

fn record_size(&self) -> usize

Returns the size of the record in bytes.
Source§

fn rtype(&self) -> Result<RType>

Tries to convert the raw record type into an enum which is useful for exhaustive pattern matching. Read more
Source§

fn publisher(&self) -> Result<Publisher>

Tries to convert the raw publisher_id into an enum which is useful for exhaustive pattern matching. Read more
Source§

fn index_ts(&self) -> Option<OffsetDateTime>

Returns the primary timestamp for the record. Returns None if the primary timestamp contains the sentinel value for a null timestamp. Read more
Source§

fn index_date(&self) -> Option<Date>

Returns the primary date for the record; the date component of the primary timestamp (index_ts()). Returns None if the primary timestamp contains the sentinel value for a null timestamp.
Source§

impl<'a> TryFrom<RecordRef<'a>> for RecordRefEnum<'a>

Source§

type Error = Error

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

fn try_from(rec_ref: RecordRef<'a>) -> Result<Self, Error>

Performs the conversion.
Source§

impl<'a> Copy for RecordRef<'a>

Source§

impl Send for RecordRef<'_>

Source§

impl Sync for RecordRef<'_>

Auto Trait Implementations§

§

impl<'a> Freeze for RecordRef<'a>

§

impl<'a> RefUnwindSafe for RecordRef<'a>

§

impl<'a> Unpin for RecordRef<'a>

§

impl<'a> UnwindSafe for RecordRef<'a>

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> 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<T> Ungil for T
where T: Send,