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>
impl<'a> RecordRef<'a>
Sourcepub unsafe fn new(buffer: &'a [u8]) -> Self
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.
Sourcepub unsafe fn unchecked_from_header(header: *const RecordHeader) -> Self
pub unsafe fn unchecked_from_header(header: *const RecordHeader) -> Self
Sourcepub fn has<T: HasRType>(&self) -> bool
pub fn has<T: HasRType>(&self) -> bool
Returns true if the object points to a record of type T.
Sourcepub fn get<T: HasRType>(&self) -> Option<&'a T>
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.
Sourcepub fn as_enum(&self) -> Result<RecordRefEnum<'_>>
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.
Trait Implementations§
Source§impl<'a> From<&'a RecordEnum> for RecordRef<'a>
impl<'a> From<&'a RecordEnum> for RecordRef<'a>
Source§fn from(rec_enum: &'a RecordEnum) -> Self
fn from(rec_enum: &'a RecordEnum) -> Self
Source§impl<'a> From<RecordRefEnum<'a>> for RecordRef<'a>
impl<'a> From<RecordRefEnum<'a>> for RecordRef<'a>
Source§fn from(rec_enum: RecordRefEnum<'a>) -> Self
fn from(rec_enum: RecordRefEnum<'a>) -> Self
Source§impl<'a> Record for RecordRef<'a>
impl<'a> Record for RecordRef<'a>
Source§fn header(&self) -> &'a RecordHeader
fn header(&self) -> &'a RecordHeader
RecordHeader that comes at the beginning of all
record types.Source§fn raw_index_ts(&self) -> u64
fn raw_index_ts(&self) -> u64
Source§fn record_size(&self) -> usize
fn record_size(&self) -> usize
Source§fn rtype(&self) -> Result<RType>
fn rtype(&self) -> Result<RType>
Source§fn publisher(&self) -> Result<Publisher>
fn publisher(&self) -> Result<Publisher>
publisher_id into an enum which is useful for
exhaustive pattern matching. Read moreSource§fn index_ts(&self) -> Option<OffsetDateTime>
fn index_ts(&self) -> Option<OffsetDateTime>
None if the primary
timestamp contains the sentinel value for a null timestamp. Read moreSource§fn index_date(&self) -> Option<Date>
fn index_date(&self) -> Option<Date>
index_ts()). Returns None if the primary timestamp contains the
sentinel value for a null timestamp.