Value

Trait Value 

Source
pub trait Value: Sized {
    type Primitive: Primitive + ?Sized;

    // Required methods
    fn from_primitive(
        value: <Self::Primitive as ToOwned>::Owned,
    ) -> Result<Self, Box<dyn Error>>;
    fn to_primitive<'a>(
        &'a self,
    ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>;
}
Expand description

A value that can be stored in a table, but must be converted first

This trait allows for any arbitrary type to be encodable and decodable from a UTF table, as long as it can be converted into one of the core storeable types (implementors of trait Primitive).

§Example

#[derive(Default)]
struct Buffer(String);

impl Value for Buffer {
    type Primitive = str;
    fn from_primitive(value: String) -> Result<Self, Box<dyn Error>> {
        Ok(Buffer(value))
    }
    fn to_primitive<'a>(&'a self) -> Result<Cow<'a, str>, Box<dyn Error>> {
        Ok(Cow::Borrowed(&self.0))
    }
}

Required Associated Types§

Source

type Primitive: Primitive + ?Sized

The primitive to which this value will be converted to/from

This may be u8, i8, u16, i16, u32, i32, u64, i64, f32, str, or [u8]

Required Methods§

Source

fn from_primitive( value: <Self::Primitive as ToOwned>::Owned, ) -> Result<Self, Box<dyn Error>>

Attempts to convert from the chosen primitive to this type.

Source

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Attempts to convert this value to the chosen primitive type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Value for f32

Source§

type Primitive = f32

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for i8

Source§

type Primitive = i8

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for i16

Source§

type Primitive = i16

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for i32

Source§

type Primitive = i32

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for i64

Source§

type Primitive = i64

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for u8

Source§

type Primitive = u8

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for u16

Source§

type Primitive = u16

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for u32

Source§

type Primitive = u32

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for u64

Source§

type Primitive = u64

Source§

fn from_primitive(value: Self) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for Box<[u8]>

Source§

type Primitive = [u8]

Source§

fn from_primitive(value: Vec<u8>) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for String

Source§

type Primitive = str

Source§

fn from_primitive(value: String) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl Value for Vec<u8>

Source§

type Primitive = [u8]

Source§

fn from_primitive(value: Vec<u8>) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Source§

impl<const N: usize> Value for [u8; N]

Source§

type Primitive = [u8]

Source§

fn from_primitive(value: Vec<u8>) -> Result<Self, Box<dyn Error>>

Source§

fn to_primitive<'a>( &'a self, ) -> Result<Cow<'a, Self::Primitive>, Box<dyn Error>>

Implementors§