#[repr(transparent)]pub struct Value<T: ValueSchema> {
pub raw: RawValue,
/* private fields */
}Expand description
A value is a 32-byte array that can be (de)serialized as a Rust type. The schema type parameter is an abstract type that represents the meaning and valid bit patterns of the bytes.
§Example
use triblespace_core::prelude::*;
use valueschemas::R256;
use num_rational::Ratio;
let ratio = Ratio::new(1, 2);
let value: Value<R256> = R256::value_from(ratio);
let ratio2: Ratio<i128> = value.try_from_value().unwrap();
assert_eq!(ratio, ratio2);Fields§
§raw: RawValueThe 32-byte representation of this value.
Implementations§
Source§impl<S: ValueSchema> Value<S>
impl<S: ValueSchema> Value<S>
Sourcepub fn new(value: RawValue) -> Self
pub fn new(value: RawValue) -> Self
Create a new value from a 32-byte array.
§Example
use triblespace_core::value::{Value, ValueSchema};
use triblespace_core::value::schemas::UnknownValue;
let bytes = [0; 32];
let value = Value::<UnknownValue>::new(bytes);Sourcepub fn validate(self) -> Result<Self, S::ValidationError>
pub fn validate(self) -> Result<Self, S::ValidationError>
Validate this value using its schema.
Sourcepub fn transmute<O>(self) -> Value<O>where
O: ValueSchema,
pub fn transmute<O>(self) -> Value<O>where
O: ValueSchema,
Transmute a value from one schema type to another. This is a safe operation, as the bytes are not changed. The schema type is only changed in the type system. This is a zero-cost operation. This is useful when you have a value with an abstract schema type, but you know the concrete schema type.
Sourcepub fn as_transmute<O>(&self) -> &Value<O>where
O: ValueSchema,
pub fn as_transmute<O>(&self) -> &Value<O>where
O: ValueSchema,
Transmute a value reference from one schema type to another. This is a safe operation, as the bytes are not changed. The schema type is only changed in the type system. This is a zero-cost operation. This is useful when you have a value reference with an abstract schema type, but you know the concrete schema type.
Sourcepub fn as_transmute_raw(value: &RawValue) -> &Self
pub fn as_transmute_raw(value: &RawValue) -> &Self
Transmute a raw value reference to a value reference.
§Example
use triblespace_core::value::{Value, ValueSchema};
use triblespace_core::value::schemas::UnknownValue;
use std::borrow::Borrow;
let bytes = [0; 32];
let value: Value<UnknownValue> = Value::new(bytes);
let value_ref: &Value<UnknownValue> = &value;
let raw_value_ref: &[u8; 32] = value_ref.borrow();
let value_ref2: &Value<UnknownValue> = Value::as_transmute_raw(raw_value_ref);
assert_eq!(&value, value_ref2);Sourcepub fn from_value<'a, T>(&'a self) -> Twhere
T: TryFromValue<'a, S, Error = Infallible>,
pub fn from_value<'a, T>(&'a self) -> Twhere
T: TryFromValue<'a, S, Error = Infallible>,
Deserialize a value with an abstract schema type to a concrete Rust type.
This method only works for infallible conversions (where Error = Infallible).
For fallible conversions, use the Value::try_from_value method.
§Example
use triblespace_core::prelude::*;
use valueschemas::F64;
let value: Value<F64> = (3.14f64).to_value();
let concrete: f64 = value.from_value();Sourcepub fn try_from_value<'a, T>(
&'a self,
) -> Result<T, <T as TryFromValue<'a, S>>::Error>where
T: TryFromValue<'a, S>,
pub fn try_from_value<'a, T>(
&'a self,
) -> Result<T, <T as TryFromValue<'a, S>>::Error>where
T: TryFromValue<'a, S>,
Deserialize a value with an abstract schema type to a concrete Rust type.
This method returns an error if the conversion is not possible. This might happen if the bytes are not valid for the schema type or if the rust type can’t represent the specific value of the schema type, e.g. if the schema type is a fractional number and the rust type is an integer.
For infallible conversions, use the Value::from_value method.
§Example
use triblespace_core::prelude::*;
use valueschemas::R256;
use num_rational::Ratio;
let value: Value<R256> = R256::value_from(Ratio::new(1, 2));
let concrete: Result<Ratio<i128>, _> = value.try_from_value();Trait Implementations§
Source§impl<T: ValueSchema> Clone for Value<T>
impl<T: ValueSchema> Clone for Value<T>
Source§impl<T: ValueSchema> Debug for Value<T>
impl<T: ValueSchema> Debug for Value<T>
Source§impl<H: HashProtocol, T: BlobSchema> From<Value<Handle<H, T>>> for Value<Hash<H>>
impl<H: HashProtocol, T: BlobSchema> From<Value<Handle<H, T>>> for Value<Hash<H>>
Source§impl<H: HashProtocol, T: BlobSchema> From<Value<Hash<H>>> for Value<Handle<H, T>>
impl<H: HashProtocol, T: BlobSchema> From<Value<Hash<H>>> for Value<Handle<H, T>>
Source§impl<T: ValueSchema> Hash for Value<T>
impl<T: ValueSchema> Hash for Value<T>
Source§impl<T: ValueSchema> IntoBytes for Value<T>
impl<T: ValueSchema> IntoBytes for Value<T>
Source§impl<T: ValueSchema> KnownLayout for Value<T>where
Self: Sized,
impl<T: ValueSchema> KnownLayout for Value<T>where
Self: Sized,
Source§type PointerMetadata = ()
type PointerMetadata = ()
Self. Read moreSource§fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>
fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>
Self with the given pointer
metadata. Read moreSource§impl<T: ValueSchema> Ord for Value<T>
impl<T: ValueSchema> Ord for Value<T>
Source§impl<T: ValueSchema> PartialEq for Value<T>
impl<T: ValueSchema> PartialEq for Value<T>
Source§impl<T: ValueSchema> PartialOrd for Value<T>
impl<T: ValueSchema> PartialOrd for Value<T>
Source§impl<S: ValueSchema> ToValue<S> for &Value<S>
impl<S: ValueSchema> ToValue<S> for &Value<S>
Source§impl<S: ValueSchema> ToValue<S> for Value<S>
impl<S: ValueSchema> ToValue<S> for Value<S>
Source§impl<T: ValueSchema> TryFromBytes for Value<T>
impl<T: ValueSchema> TryFromBytes for Value<T>
Source§fn try_ref_from_bytes(
source: &[u8],
) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: KnownLayout + Immutable,
fn try_ref_from_bytes(
source: &[u8],
) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: KnownLayout + Immutable,
Source§fn try_ref_from_prefix(
source: &[u8],
) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: KnownLayout + Immutable,
fn try_ref_from_prefix(
source: &[u8],
) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: KnownLayout + Immutable,
Source§fn try_ref_from_suffix(
source: &[u8],
) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: KnownLayout + Immutable,
fn try_ref_from_suffix(
source: &[u8],
) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: KnownLayout + Immutable,
Source§fn try_mut_from_bytes(
bytes: &mut [u8],
) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>where
Self: KnownLayout + IntoBytes,
fn try_mut_from_bytes(
bytes: &mut [u8],
) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>where
Self: KnownLayout + IntoBytes,
Source§fn try_mut_from_prefix(
source: &mut [u8],
) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>where
Self: KnownLayout + IntoBytes,
fn try_mut_from_prefix(
source: &mut [u8],
) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>where
Self: KnownLayout + IntoBytes,
Source§fn try_mut_from_suffix(
source: &mut [u8],
) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>where
Self: KnownLayout + IntoBytes,
fn try_mut_from_suffix(
source: &mut [u8],
) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>where
Self: KnownLayout + IntoBytes,
Source§fn try_read_from_bytes(
source: &[u8],
) -> Result<Self, ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
fn try_read_from_bytes(
source: &[u8],
) -> Result<Self, ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
Source§fn try_read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
fn try_read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
Source§fn try_read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
fn try_read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
Source§impl<'a, S: ValueSchema> TryFromValue<'a, S> for Value<S>
impl<'a, S: ValueSchema> TryFromValue<'a, S> for Value<S>
Source§type Error = Infallible
type Error = Infallible
Source§fn try_from_value(v: &'a Value<S>) -> Result<Self, Infallible>
fn try_from_value(v: &'a Value<S>) -> Result<Self, Infallible>
impl<T: ValueSchema> Copy for Value<T>
impl<T: ValueSchema> Eq for Value<T>
impl<T: ValueSchema> Immutable for Value<T>
impl<T: ValueSchema> Unaligned for Value<T>
Auto Trait Implementations§
impl<T> Freeze for Value<T>
impl<T> RefUnwindSafe for Value<T>where
T: RefUnwindSafe,
impl<T> Send for Value<T>where
T: Send,
impl<T> Sync for Value<T>where
T: Sync,
impl<T> Unpin for Value<T>where
T: Unpin,
impl<T> UnsafeUnpin for Value<T>
impl<T> UnwindSafe for Value<T>where
T: UnwindSafe,
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,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more