pub enum FieldValue<'a> {
Null,
Text(&'a str),
}Expand description
The value of one field of one item.
TLCP distinguishes null from the empty string, and so does this
type: # on the wire produces FieldValue::Null and $ produces
FieldValue::Text wrapping "" [docs/spec/04-notifications.md §2.2].
They are not interchangeable. A quote adapter that has no bid to report
sends #; one whose status line is deliberately blank sends $.
§Examples
An ItemUpdate is obtained from a live subscription, so the example is
written as the function you would pass one to — which compiles, and so
keeps saying something true:
use lightstreamer_rs::{FieldValue, ItemUpdate};
fn report_bid(update: &ItemUpdate) {
match update.field_by_name("bid") {
Some(FieldValue::Text(bid)) => println!("bid is {bid}"),
Some(FieldValue::Null) => println!("there is no bid"),
None => println!("this subscription has no `bid` field"),
}
}The three arms are the three answers, and they are not
interchangeable: Text("") is a fourth thing again — a value that happens
to be empty. Use is_empty_text to ask.
Variants§
Null
The field has no value — the # marker
[docs/spec/04-notifications.md §2.2].
Also what a field reads as before any update has ever set it, which is
only observable between the subscription being activated and its first
U: the first update of a subscription carries a token for every field
[docs/spec/04-notifications.md §2.3].
Text(&'a str)
The field holds this text, which may be the empty string — the $
marker sets it to exactly that
[docs/spec/04-notifications.md §2.2].
Implementations§
Source§impl<'a> FieldValue<'a>
impl<'a> FieldValue<'a>
Sourcepub const fn is_null(self) -> bool
pub const fn is_null(self) -> bool
Whether the field is null, i.e. has no value at all.
A field holding the empty string is not null.
Sourcepub fn is_empty_text(self) -> bool
pub fn is_empty_text(self) -> bool
Whether the field holds the empty string.
A null field is not empty text: it has no text.
Sourcepub const fn text(self) -> Option<&'a str>
pub const fn text(self) -> Option<&'a str>
The text, or None if the field is null.
Collapsing the distinction is a deliberate act here, which is the point:
text() reads as “I am choosing to treat null as absent”, whereas an
accessor that returned &str directly would make the same choice
silently.
Sourcepub fn text_or(self, default: &'a str) -> &'a str
pub fn text_or(self, default: &'a str) -> &'a str
The text, substituting default if the field is null.
This is how a field value is rendered for a human: Display is
deliberately not implemented, because every rendering of a null has
to choose a spelling and no choice is safe to make on the caller’s
behalf — the empty string collapses null into $, and any placeholder
is a string a field could legitimately hold. Naming the substitute at
the call site is a one-word cost and it keeps
println!("{}", value.text_or("(null)")) honest.
Trait Implementations§
Source§impl<'a> Clone for FieldValue<'a>
impl<'a> Clone for FieldValue<'a>
Source§fn clone(&self) -> FieldValue<'a>
fn clone(&self) -> FieldValue<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more