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.
Can hold any type implementing HasRType and acts similar to a &dyn HasRType
or &dyn Record but due to the design of DBN records, only a few methods require a
dynamic dispatch.
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.
§Examples
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.
Usually paired with get() or get_unchecked().
rtype matches that of the type T. It does not check
the length.
Use try_get() when working with different versions of a DBN
struct.
§Examples
use dbn::{OhlcvMsg, RecordRef, Schema, TradeMsg};
let bar = OhlcvMsg::default_for_schema(Schema::Ohlcv1M);
let rec = RecordRef::from(&bar);
// This is a bar
assert!(rec.has::<OhlcvMsg>());
// It's not a trade
assert!(!rec.has::<TradeMsg>());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. Use try_get()
to more gracefully handle versioned structs and the optional presence of crate::WithTsOut.
§Examples
use dbn::{BboMsg, RecordRef, Schema};
let bbo = BboMsg::default_for_schema(Schema::Bbo1S);
let rec = RecordRef::from(&bbo);
if let Some(bbo) = rec.get::<BboMsg>() {
println!("{bbo:?}");
}With versioned DBN structs
use dbn::{v1, v2, RecordRef};
// Initialize with version 1 definition
let def = v1::InstrumentDefMsg::default();
let rec = RecordRef::from(&def);
// Try to extract a version 2 definition
let _def = rec.get::<v2::InstrumentDefMsg>();Sourcepub fn try_get<T: HasRType>(&self) -> Result<&'a T>
pub fn try_get<T: HasRType>(&self) -> Result<&'a T>
Like get(), but returns an error if the inner record is not a T
or has the correct rtype for T, but insufficient length. Never panics
§Errors
This function returns an error if does not hold a T or if its rtype matches
T, but its length is too short.
§Examples
use dbn::{v1, v2, v3, RecordRef, WithTsOut};
// Initialize with version 1 definition
let def = v1::InstrumentDefMsg::default();
let rec = RecordRef::from(&def);
// Try to extract new versions of definitions
assert!(rec.try_get::<v2::InstrumentDefMsg>().is_err());
assert!(rec.try_get::<v3::InstrumentDefMsg>().is_err());
rec.try_get::<v1::InstrumentDefMsg>().unwrap();
// Also works with data that might have ts_out
assert!(rec.try_get::<WithTsOut<v1::InstrumentDefMsg>>().is_err());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.
Sourcepub unsafe fn get_unchecked<T: HasRType>(&self) -> &'a T
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.
§Examples
use dbn::{BboMsg, RecordRef, Schema};
let bbo = BboMsg::default_for_schema(Schema::Bbo1S);
let rec = RecordRef::from(&bbo);
if rec.has::<BboMsg>() {
// SAFETY: checked rtype
println!("{:?}", unsafe { rec.get_unchecked::<BboMsg>() });
}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.