pub struct Version;
Expand description
This represents the JSON-RPC version.
This is an empty marker type that always gets (de)serialized as Self::TWO
.
It is the only valid value for the jsonrpc
field in the
Request
and Response
objects.
JSON-RPC 2.0 allows for backwards compatibility with 1.0
but this crate
(and this type) will not accept that, and will fail in deserialization
when encountering anything but Self::TWO
.
§Formatting
When using Rust formatting, Version
is formatted as 2.0
.
When using JSON serialization, Version
is formatted with quotes indicating
it is a JSON string and not a JSON float, i.e. it gets formatted as "2.0"
, not 2.0
.
§Example
use cuprate_json_rpc::Version;
use serde_json::{to_string, to_string_pretty, from_str};
assert_eq!(Version::TWO, "2.0");
let version = Version;
// All debug/display formats are the same.
assert_eq!(format!("{version:?}"), Version::TWO);
assert_eq!(format!("{version:#?}"), Version::TWO);
assert_eq!(format!("{version}"), Version::TWO);
// JSON serialization will add extra quotes to
// indicate it is a string and not a float.
assert_eq!(to_string(&Version).unwrap(), "\"2.0\"");
assert_eq!(to_string_pretty(&Version).unwrap(), "\"2.0\"");
// Deserialization only accepts the JSON string "2.0".
assert!(from_str::<Version>(&"\"2.0\"").is_ok());
// This is JSON float, not a string.
assert!(from_str::<Version>(&"2.0").is_err());
assert!(from_str::<Version>(&"2").is_err());
assert!(from_str::<Version>(&"1.0").is_err());
assert!(from_str::<Version>(&"20").is_err());
assert!(from_str::<Version>(&"two").is_err());
assert!(from_str::<Version>(&"2.1").is_err());
assert!(from_str::<Version>(&"v2.0").is_err());
assert!(from_str::<Version>("").is_err());
Implementations§
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Version
impl<'de> Deserialize<'de> for Version
Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Source§impl Ord for Version
impl Ord for Version
Source§impl PartialOrd for Version
impl PartialOrd for Version
impl Copy for Version
impl Eq for Version
impl StructuralPartialEq for Version
Auto Trait Implementations§
impl Freeze for Version
impl RefUnwindSafe for Version
impl Send for Version
impl Sync for Version
impl Unpin for Version
impl UnwindSafe for Version
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: 0 bytes