pub enum Id {
Null,
Num(u64),
Str(Cow<'static, str>),
}
Expand description
Request/Response identification.
This is the JSON-RPC 2.0 id
field
type found in Request/Response
s.
§From
This type implements From
on:
and all of those wrapped in Option
.
If the Option
is None
, Id::Null
is returned.
Note that the &str
implementations will allocate, use Id::from_static_str
(or just manually create the Cow
) for a non-allocating Id
.
use cuprate_json_rpc::Id;
assert_eq!(Id::from(String::new()), Id::Str("".into()));
assert_eq!(Id::from(Some(String::new())), Id::Str("".into()));
assert_eq!(Id::from(None::<String>), Id::Null);
assert_eq!(Id::from(123_u64), Id::Num(123_u64));
assert_eq!(Id::from(Some(123_u64)), Id::Num(123_u64));
assert_eq!(Id::from(None::<u64>), Id::Null);
Variants§
Null
A JSON null
value.
use cuprate_json_rpc::Id;
use serde_json::{from_value,to_value,json,Value};
assert_eq!(from_value::<Id>(json!(null)).unwrap(), Id::Null);
assert_eq!(to_value(Id::Null).unwrap(), Value::Null);
// Not a real `null`, but a string.
assert_eq!(from_value::<Id>(json!("null")).unwrap(), Id::Str("null".into()));
Num(u64)
A JSON number
value.
Str(Cow<'static, str>)
A JSON string
value.
This is a Cow<'static, str>
to support both 0-allocation for
const
string ID’s commonly found in programs, as well as support
for runtime String
’s.
use std::borrow::Cow;
use cuprate_json_rpc::Id;
/// A program's static ID.
const ID: &'static str = "my_id";
// No allocation.
let s = Id::Str(Cow::Borrowed(ID));
// Runtime allocation.
let s = Id::Str(Cow::Owned("runtime_id".to_string()));
Implementations§
Source§impl Id
impl Id
Sourcepub const fn as_u64(&self) -> Option<u64>
pub const fn as_u64(&self) -> Option<u64>
This returns Some(u64)
if Id
is a number.
use cuprate_json_rpc::Id;
assert_eq!(Id::Num(0).as_u64(), Some(0));
assert_eq!(Id::Str("0".into()).as_u64(), None);
assert_eq!(Id::Null.as_u64(), None);
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
This returns Some(&str)
if Id
is a string.
use cuprate_json_rpc::Id;
assert_eq!(Id::Str("0".into()).as_str(), Some("0"));
assert_eq!(Id::Num(0).as_str(), None);
assert_eq!(Id::Null.as_str(), None);
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true
if self
is Id::Null
.
use cuprate_json_rpc::Id;
assert!(Id::Null.is_null());
assert!(!Id::Num(0).is_null());
assert!(!Id::Str("".into()).is_null());
Sourcepub const fn from_static_str(s: &'static str) -> Self
pub const fn from_static_str(s: &'static str) -> Self
Create a new Id::Str
from a static string.
use cuprate_json_rpc::Id;
assert_eq!(Id::from_static_str("hi"), Id::Str("hi".into()));
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Id
impl<'de> Deserialize<'de> for Id
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Ord for Id
impl Ord for Id
Source§impl PartialOrd for Id
impl PartialOrd for Id
impl Eq for Id
impl StructuralPartialEq for Id
Auto Trait Implementations§
impl Freeze for Id
impl RefUnwindSafe for Id
impl Send for Id
impl Sync for Id
impl Unpin for Id
impl UnwindSafe for Id
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 24 bytes
Size for each variant:
Null
: 0 bytesNum
: 16 bytesStr
: 24 bytes