Skip to main content

Value

Struct Value 

Source
#[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: RawValue

The 32-byte representation of this value.

Implementations§

Source§

impl<S: ValueSchema> Value<S>

Source

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);
Source

pub fn validate(self) -> Result<Self, S::ValidationError>

Validate this value using its schema.

Source

pub fn is_valid(&self) -> bool

Check if this value conforms to its schema.

Source

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.

Source

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.

Source

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);
Source

pub fn from_value<'a, T>(&'a self) -> T
where 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();
Source

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<S: ValueSchema> Borrow<[u8; 32]> for Value<S>

Source§

fn borrow(&self) -> &RawValue

Immutably borrows from an owned value. Read more
Source§

impl<T: ValueSchema> Clone for Value<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: ValueSchema> Debug for Value<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<H: HashProtocol, T: BlobSchema> From<Value<Handle<H, T>>> for Value<Hash<H>>

Source§

fn from(value: Value<Handle<H, T>>) -> Self

Converts to this type from the input type.
Source§

impl<H: HashProtocol, T: BlobSchema> From<Value<Hash<H>>> for Value<Handle<H, T>>

Source§

fn from(value: Value<Hash<H>>) -> Self

Converts to this type from the input type.
Source§

impl<T: ValueSchema> Hash for Value<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: ValueSchema> IntoBytes for Value<T>

Source§

fn as_bytes(&self) -> &[u8]
where Self: Immutable,

Gets the bytes of this value. Read more
Source§

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
Source§

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
Source§

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
Source§

impl<T: ValueSchema> KnownLayout for Value<T>
where Self: Sized,

Source§

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
Source§

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
Source§

impl<T: ValueSchema> Ord for Value<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: ValueSchema> PartialEq for Value<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: ValueSchema> PartialOrd for Value<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<S: ValueSchema> ToValue<S> for &Value<S>

Source§

fn to_value(self) -> Value<S>

Convert the Rust type to a Value with a specific schema type. This might cause a panic if the conversion is not possible. Read more
Source§

impl<S: ValueSchema> ToValue<S> for Value<S>

Source§

fn to_value(self) -> Value<S>

Convert the Rust type to a Value with a specific schema type. This might cause a panic if the conversion is not possible. Read more
Source§

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,

Attempts to interpret the given source as a &Self. Read more
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,

Attempts to interpret the prefix of the given source as a &Self. Read more
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,

Attempts to interpret the suffix of the given source as a &Self. Read more
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,

Attempts to interpret the given source as a &mut Self without copying. Read more
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,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
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,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
Source§

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
Source§

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
Source§

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
Source§

impl<'a, S: ValueSchema> TryFromValue<'a, S> for Value<S>

Source§

type Error = Infallible

The error type returned when the conversion fails.
Source§

fn try_from_value(v: &'a Value<S>) -> Result<Self, Infallible>

Convert the Value with a specific schema type to the Rust type.
Source§

impl<T: ValueSchema> Copy for Value<T>

Source§

impl<T: ValueSchema> Eq for Value<T>

Source§

impl<T: ValueSchema> Immutable for Value<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V