Enum valuable::Value

source ·
#[non_exhaustive]
pub enum Value<'a> {
Show 25 variants Bool(bool), Char(char), F32(f32), F64(f64), I8(i8), I16(i16), I32(i32), I64(i64), I128(i128), Isize(isize), String(&'a str), U8(u8), U16(u16), U32(u32), U64(u64), U128(u128), Usize(usize), Path(&'a Path), Error(&'a (dyn Error + 'static)), Listable(&'a dyn Listable), Mappable(&'a dyn Mappable), Structable(&'a dyn Structable), Enumerable(&'a dyn Enumerable), Tuplable(&'a dyn Tuplable), Unit,
}
Expand description

Any Rust value

The Value enum is used to pass single values to the visitor. Primitive types are enumerated and other types are represented at trait objects.

Values are converted to Value instances using Valuable::as_value().

Examples

Convert a primitive type

use valuable::{Value, Valuable};

let num = 123;
let val = num.as_value();

assert!(matches!(val, Value::I32(v) if v == 123));

Converting a struct

use valuable::{Value, Valuable};

#[derive(Valuable, Debug)]
struct HelloWorld {
    message: String,
}

let hello = HelloWorld {
    message: "greetings".to_string(),
};

let val = hello.as_value();

assert!(matches!(val, Value::Structable(_v)));

// The Value `Debug` output matches the struct's
assert_eq!(
    format!("{:?}", val),
    format!("{:?}", hello),
);

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Bool(bool)

A Rust bool value

Examples

use valuable::Value;

let v = Value::Bool(true);
§

Char(char)

A Rust char value

Examples

use valuable::Value;

let v = Value::Char('h');
§

F32(f32)

A Rust f32 value

Examples

use valuable::Value;

let v = Value::F32(3.1415);
§

F64(f64)

A Rust f64 value

Examples

use valuable::Value;

let v = Value::F64(3.1415);
§

I8(i8)

A Rust i8 value

Examples

use valuable::Value;

let v = Value::I8(42);
§

I16(i16)

A Rust i16 value

Examples

use valuable::Value;

let v = Value::I16(42);
§

I32(i32)

A Rust i32 value

Examples

use valuable::Value;

let v = Value::I32(42);
§

I64(i64)

A Rust i64 value

Examples

use valuable::Value;

let v = Value::I64(42);
§

I128(i128)

A Rust i128 value

Examples

use valuable::Value;

let v = Value::I128(42);
§

Isize(isize)

A Rust isize value

Examples

use valuable::Value;

let v = Value::Isize(42);
§

String(&'a str)

A Rust &str value

Examples

use valuable::Value;

let v = Value::String("hello");
§

U8(u8)

A Rust u8 value

Examples

use valuable::Value;

let v = Value::U8(42);
§

U16(u16)

A Rust u16 value

Examples

use valuable::Value;

let v = Value::U16(42);
§

U32(u32)

A Rust u32 value

Examples

use valuable::Value;

let v = Value::U32(42);
§

U64(u64)

A Rust u64 value

Examples

use valuable::Value;

let v = Value::U64(42);
§

U128(u128)

A Rust u128 value

Examples

use valuable::Value;

let v = Value::U128(42);
§

Usize(usize)

A Rust usize value

Examples

use valuable::Value;

let v = Value::Usize(42);
§

Path(&'a Path)

A Rust &Path value

Examples

use valuable::Value;
use std::path::Path;

let path = Path::new("a.txt");
let v = Value::Path(path);
§

Error(&'a (dyn Error + 'static))

A Rust error value

Examples

use valuable::Value;
use std::io;

let err: io::Error = io::ErrorKind::Other.into();
let v = Value::Error(&err);
§

Listable(&'a dyn Listable)

A Rust list value

Examples

use valuable::Value;

let vals = vec![1, 2, 3, 4, 5];
let v = Value::Listable(&vals);
§

Mappable(&'a dyn Mappable)

A Rust map value

Examples

use valuable::Value;
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("foo", 1);
map.insert("bar", 2);

let v = Value::Mappable(&map);
§

Structable(&'a dyn Structable)

A Rust struct value

Examples

use valuable::{Value, Valuable};

#[derive(Valuable)]
struct MyStruct {
    field: u32,
}

let my_struct = MyStruct {
    field: 123,
};

let v = Value::Structable(&my_struct);
§

Enumerable(&'a dyn Enumerable)

A Rust enum value

Examples

use valuable::{Value, Valuable};

#[derive(Valuable)]
enum MyEnum {
    Foo,
    Bar,
}

let my_enum = MyEnum::Foo;
let v = Value::Enumerable(&my_enum);
§

Tuplable(&'a dyn Tuplable)

A tuple value

Examples

use valuable::{Value, Valuable};

let my_tuple = (123, 456);
let v = Value::Tuplable(&my_tuple);
§

Unit

A Rust () or None value.

Examples

use valuable::Value;

let v = Value::Unit;

Implementations§

source§

impl<'a> Value<'a>

source

pub fn as_bool(&self) -> Option<bool>

Return a bool representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::Bool(true).as_bool(), Some(true));
assert_eq!(Value::Char('c').as_bool(), None);
source

pub fn as_char(&self) -> Option<char>

Return a char representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::Char('c').as_char(), Some('c'));
assert_eq!(Value::Bool(true).as_char(), None);
source

pub fn as_f32(&self) -> Option<f32>

Return a f32 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::F32(3.1415).as_f32(), Some(3.1415));
assert_eq!(Value::Bool(true).as_f32(), None);
source

pub fn as_f64(&self) -> Option<f64>

Return a f64 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::F64(3.1415).as_f64(), Some(3.1415));
assert_eq!(Value::Bool(true).as_f64(), None);
source

pub fn as_i8(&self) -> Option<i8>

Return a i8 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::I8(42).as_i8(), Some(42));
assert_eq!(Value::I32(42).as_i8(), Some(42));

assert_eq!(Value::I64(i64::MAX).as_i8(), None);
assert_eq!(Value::Bool(true).as_i8(), None);
source

pub fn as_i16(&self) -> Option<i16>

Return a i16 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::I16(42).as_i16(), Some(42));
assert_eq!(Value::I32(42).as_i16(), Some(42));

assert_eq!(Value::I64(i64::MAX).as_i16(), None);
assert_eq!(Value::Bool(true).as_i16(), None);
source

pub fn as_i32(&self) -> Option<i32>

Return a i32 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::I32(42).as_i32(), Some(42));
assert_eq!(Value::I64(42).as_i32(), Some(42));

assert_eq!(Value::I64(i64::MAX).as_i32(), None);
assert_eq!(Value::Bool(true).as_i32(), None);
source

pub fn as_i64(&self) -> Option<i64>

Return a i64 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::I64(42).as_i64(), Some(42));
assert_eq!(Value::I128(42).as_i64(), Some(42));

assert_eq!(Value::I128(i128::MAX).as_i64(), None);
assert_eq!(Value::Bool(true).as_i64(), None);
source

pub fn as_i128(&self) -> Option<i128>

Return a i128 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::I128(42).as_i128(), Some(42));
assert_eq!(Value::U128(42).as_i128(), Some(42));

assert_eq!(Value::U128(u128::MAX).as_i128(), None);
assert_eq!(Value::Bool(true).as_i128(), None);
source

pub fn as_isize(&self) -> Option<isize>

Return a isize representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::Isize(42).as_isize(), Some(42));
assert_eq!(Value::Usize(42).as_isize(), Some(42));

assert_eq!(Value::Usize(usize::MAX).as_isize(), None);
assert_eq!(Value::Bool(true).as_isize(), None);
source

pub fn as_u8(&self) -> Option<u8>

Return a u8 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::U8(42).as_u8(), Some(42));
assert_eq!(Value::U32(42).as_u8(), Some(42));

assert_eq!(Value::U32(u32::MAX).as_u8(), None);
assert_eq!(Value::Bool(true).as_u8(), None);
source

pub fn as_u16(&self) -> Option<u16>

Return a u16 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::U16(42).as_u16(), Some(42));
assert_eq!(Value::U32(42).as_u16(), Some(42));

assert_eq!(Value::U32(u32::MAX).as_u16(), None);
assert_eq!(Value::Bool(true).as_u16(), None);
source

pub fn as_u32(&self) -> Option<u32>

Return a u32 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::U32(42).as_u32(), Some(42));
assert_eq!(Value::U64(42).as_u32(), Some(42));

assert_eq!(Value::U64(u64::MAX).as_u32(), None);
assert_eq!(Value::Bool(true).as_u32(), None);
source

pub fn as_u64(&self) -> Option<u64>

Return a u64 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::U64(42).as_u64(), Some(42));
assert_eq!(Value::U128(42).as_u64(), Some(42));

assert_eq!(Value::U128(u128::MAX).as_u64(), None);
assert_eq!(Value::Bool(true).as_u64(), None);
source

pub fn as_u128(&self) -> Option<u128>

Return a u128 representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::U128(42).as_u128(), Some(42));
assert_eq!(Value::I32(42).as_u128(), Some(42));

assert_eq!(Value::I32(-5).as_u128(), None);
assert_eq!(Value::Bool(true).as_u128(), None);
source

pub fn as_usize(&self) -> Option<usize>

Return a usize representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::Usize(42).as_usize(), Some(42));
assert_eq!(Value::I8(42).as_usize(), Some(42));

assert_eq!(Value::I8(-5).as_usize(), None);
assert_eq!(Value::Bool(true).as_usize(), None);
source

pub fn as_str(&self) -> Option<&str>

Return a &str representation of self, if possible.

Examples
use valuable::Value;

assert_eq!(Value::String("hello").as_str(), Some("hello"));
assert_eq!(Value::Bool(true).as_str(), None);
source

pub fn as_path(&self) -> Option<&Path>

Return a &Path representation of self, if possible.

Examples
use valuable::Value;
use std::path::Path;

let path = Path::new("a.txt");

assert!(Value::Path(path).as_path().is_some());
assert!(Value::Bool(true).as_path().is_none());
source

pub fn as_error(&self) -> Option<&(dyn Error + 'static)>

Return a &dyn Error representation of self, if possible.

Examples
use valuable::Value;
use std::io;

let err: io::Error = io::ErrorKind::Other.into();

assert!(Value::Error(&err).as_error().is_some());
assert!(Value::Bool(true).as_error().is_none());
source

pub fn as_listable(&self) -> Option<&dyn Listable>

Return a &dyn Listable representation of self, if possible.

Examples
use valuable::Value;

let list = vec![1, 2, 3, 4];

assert!(Value::Listable(&list).as_listable().is_some());
assert!(Value::Bool(true).as_listable().is_none());
source

pub fn as_mappable(&self) -> Option<&dyn Mappable>

Return a &dyn Mappable representation of self, if possible.

Examples
use valuable::Value;
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("foo", 123);
map.insert("bar", 456);

assert!(Value::Mappable(&map).as_mappable().is_some());
assert!(Value::Bool(true).as_mappable().is_none());
source

pub fn as_structable(&self) -> Option<&dyn Structable>

Return a &dyn Structable representation of self, if possible.

Examples
use valuable::{Value, Valuable};

#[derive(Valuable)]
struct Hello {
    message: &'static str,
}

let hello = Hello { message: "Hello world" };

assert!(Value::Structable(&hello).as_structable().is_some());
assert!(Value::Bool(true).as_structable().is_none());
source

pub fn as_enumerable(&self) -> Option<&dyn Enumerable>

Return a &dyn Enumerable representation of self, if possible.

Examples
use valuable::{Value, Valuable};

#[derive(Valuable)]
enum Greet {
    Hello,
    World,
}

let greet = Greet::Hello;

assert!(Value::Enumerable(&greet).as_enumerable().is_some());
assert!(Value::Bool(true).as_enumerable().is_none());
source

pub fn as_tuplable(&self) -> Option<&dyn Tuplable>

Return a &dyn Tuplable representation of self, if possible.

Examples
use valuable::Value;

let my_tuple = (123, 456);

assert!(Value::Tuplable(&my_tuple).as_tuplable().is_some());
assert!(Value::Bool(true).as_tuplable().is_none());

Trait Implementations§

source§

impl<'a> Clone for Value<'a>

source§

fn clone(&self) -> Value<'a>

Returns a copy 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 Debug for Value<'_>

source§

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

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

impl Default for Value<'_>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'a> From<&'a (dyn Enumerable + 'a)> for Value<'a>

A Rust enum value

Examples

use valuable::{Value, Valuable};

#[derive(Valuable)]
enum MyEnum {
    Foo,
    Bar,
}

let my_enum = MyEnum::Foo;
let v = Value::Enumerable(&my_enum);
source§

fn from(src: &'a dyn Enumerable) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<&'a (dyn Error + 'static)> for Value<'a>

A Rust error value

Examples

use valuable::Value;
use std::io;

let err: io::Error = io::ErrorKind::Other.into();
let v = Value::Error(&err);
source§

fn from(src: &'a (dyn Error + 'static)) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<&'a (dyn Listable + 'a)> for Value<'a>

A Rust list value

Examples

use valuable::Value;

let vals = vec![1, 2, 3, 4, 5];
let v = Value::Listable(&vals);
source§

fn from(src: &'a dyn Listable) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<&'a (dyn Mappable + 'a)> for Value<'a>

A Rust map value

Examples

use valuable::Value;
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("foo", 1);
map.insert("bar", 2);

let v = Value::Mappable(&map);
source§

fn from(src: &'a dyn Mappable) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<&'a (dyn Structable + 'a)> for Value<'a>

A Rust struct value

Examples

use valuable::{Value, Valuable};

#[derive(Valuable)]
struct MyStruct {
    field: u32,
}

let my_struct = MyStruct {
    field: 123,
};

let v = Value::Structable(&my_struct);
source§

fn from(src: &'a dyn Structable) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<&'a (dyn Tuplable + 'a)> for Value<'a>

A tuple value

Examples

use valuable::{Value, Valuable};

let my_tuple = (123, 456);
let v = Value::Tuplable(&my_tuple);
source§

fn from(src: &'a dyn Tuplable) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<&'a Path> for Value<'a>

A Rust &Path value

Examples

use valuable::Value;
use std::path::Path;

let path = Path::new("a.txt");
let v = Value::Path(path);
source§

fn from(src: &'a Path) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<&'a str> for Value<'a>

A Rust &str value

Examples

use valuable::Value;

let v = Value::String("hello");
source§

fn from(src: &'a str) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<()> for Value<'a>

source§

fn from(_: ()) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<bool> for Value<'a>

A Rust bool value

Examples

use valuable::Value;

let v = Value::Bool(true);
source§

fn from(src: bool) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<char> for Value<'a>

A Rust char value

Examples

use valuable::Value;

let v = Value::Char('h');
source§

fn from(src: char) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<f32> for Value<'a>

A Rust f32 value

Examples

use valuable::Value;

let v = Value::F32(3.1415);
source§

fn from(src: f32) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<f64> for Value<'a>

A Rust f64 value

Examples

use valuable::Value;

let v = Value::F64(3.1415);
source§

fn from(src: f64) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<i128> for Value<'a>

A Rust i128 value

Examples

use valuable::Value;

let v = Value::I128(42);
source§

fn from(src: i128) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<i16> for Value<'a>

A Rust i16 value

Examples

use valuable::Value;

let v = Value::I16(42);
source§

fn from(src: i16) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<i32> for Value<'a>

A Rust i32 value

Examples

use valuable::Value;

let v = Value::I32(42);
source§

fn from(src: i32) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<i64> for Value<'a>

A Rust i64 value

Examples

use valuable::Value;

let v = Value::I64(42);
source§

fn from(src: i64) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<i8> for Value<'a>

A Rust i8 value

Examples

use valuable::Value;

let v = Value::I8(42);
source§

fn from(src: i8) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<isize> for Value<'a>

A Rust isize value

Examples

use valuable::Value;

let v = Value::Isize(42);
source§

fn from(src: isize) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<u128> for Value<'a>

A Rust u128 value

Examples

use valuable::Value;

let v = Value::U128(42);
source§

fn from(src: u128) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<u16> for Value<'a>

A Rust u16 value

Examples

use valuable::Value;

let v = Value::U16(42);
source§

fn from(src: u16) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<u32> for Value<'a>

A Rust u32 value

Examples

use valuable::Value;

let v = Value::U32(42);
source§

fn from(src: u32) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<u64> for Value<'a>

A Rust u64 value

Examples

use valuable::Value;

let v = Value::U64(42);
source§

fn from(src: u64) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<u8> for Value<'a>

A Rust u8 value

Examples

use valuable::Value;

let v = Value::U8(42);
source§

fn from(src: u8) -> Value<'a>

Converts to this type from the input type.
source§

impl<'a> From<usize> for Value<'a>

A Rust usize value

Examples

use valuable::Value;

let v = Value::Usize(42);
source§

fn from(src: usize) -> Value<'a>

Converts to this type from the input type.
source§

impl Valuable for Value<'_>

source§

fn as_value(&self) -> Value<'_>

Converts self into a Value instance. Read more
source§

fn visit(&self, visit: &mut dyn Visit)

Calls the relevant method on Visit to extract data from self. Read more
source§

fn visit_slice(slice: &[Self], visit: &mut dyn Visit)where Self: Sized,

source§

impl<'a> Copy for Value<'a>

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Value<'a>

§

impl<'a> !Send for Value<'a>

§

impl<'a> !Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> !UnwindSafe for Value<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.