Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(String),
    Bytes(Vec<u8>),
    Array(Vec<Value>),
    Object(Document),
}
Expand description

A single field value inside a Document.

Value is deliberately small and closed: it models the shapes a schemaless document store needs and nothing more. Nesting is expressed through Value::Array and Value::Object, so an arbitrarily deep record is just a tree of Values.

§Examples

use bison_db::Value;

let v = Value::from("hello");
assert_eq!(v.as_str(), Some("hello"));
assert!(Value::from(42_i64).as_int() == Some(42));
assert!(Value::Null.is_null());

Variants§

§

Null

The absence of a value.

§

Bool(bool)

A boolean.

§

Int(i64)

A signed 64-bit integer. All integral fields are stored at this width.

§

Float(f64)

A 64-bit IEEE-754 float.

§

Str(String)

A UTF-8 string.

§

Bytes(Vec<u8>)

An opaque byte string, for binary fields that are not valid UTF-8.

§

Array(Vec<Value>)

An ordered list of values.

§

Object(Document)

A nested document.

Implementations§

Source§

impl Value

Source

pub const fn is_null(&self) -> bool

Returns true if this value is Value::Null.

§Examples
use bison_db::Value;
assert!(Value::Null.is_null());
assert!(!Value::from(0_i64).is_null());
Source

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

Returns the boolean if this is a Value::Bool, otherwise None.

§Examples
use bison_db::Value;
assert_eq!(Value::from(true).as_bool(), Some(true));
assert_eq!(Value::from(1_i64).as_bool(), None);
Source

pub const fn as_int(&self) -> Option<i64>

Returns the integer if this is a Value::Int, otherwise None.

Floats are not coerced; a Value::Float returns None.

§Examples
use bison_db::Value;
assert_eq!(Value::from(7_i64).as_int(), Some(7));
assert_eq!(Value::from(7.0_f64).as_int(), None);
Source

pub const fn as_float(&self) -> Option<f64>

Returns the float if this is a Value::Float, otherwise None.

§Examples
use bison_db::Value;
assert_eq!(Value::from(1.5_f64).as_float(), Some(1.5));
assert_eq!(Value::from(1_i64).as_float(), None);
Source

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

Returns the string slice if this is a Value::Str, otherwise None.

§Examples
use bison_db::Value;
assert_eq!(Value::from("bison").as_str(), Some("bison"));
Source

pub fn as_bytes(&self) -> Option<&[u8]>

Returns the byte slice if this is a Value::Bytes, otherwise None.

§Examples
use bison_db::Value;
assert_eq!(Value::Bytes(vec![1, 2, 3]).as_bytes(), Some(&[1, 2, 3][..]));
Source

pub fn as_array(&self) -> Option<&[Value]>

Returns the element slice if this is a Value::Array, otherwise None.

§Examples
use bison_db::Value;
let v = Value::Array(vec![Value::from(1_i64), Value::from(2_i64)]);
assert_eq!(v.as_array().map(<[_]>::len), Some(2));
Source

pub fn as_object(&self) -> Option<&Document>

Returns the nested document if this is a Value::Object, otherwise None.

§Examples
use bison_db::{Document, Value};
let mut inner = Document::new();
inner.set("k", 1_i64);
let v = Value::Object(inner);
assert_eq!(v.as_object().and_then(|d| d.get("k")).and_then(Value::as_int), Some(1));
Source

pub const fn type_name(&self) -> &'static str

Returns a short, stable name for the value’s variant.

Intended for diagnostics and error messages, not for logic; match on the variant directly when behaviour depends on the type.

§Examples
use bison_db::Value;
assert_eq!(Value::from(1_i64).type_name(), "int");
assert_eq!(Value::Null.type_name(), "null");

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

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

impl Default for Value

Source§

fn default() -> Value

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

impl<'de> Deserialize<'de> for Value

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&str> for Value

Source§

fn from(v: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Document> for Value

Source§

fn from(v: Document) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Value>> From<Option<T>> for Value

Source§

fn from(v: Option<T>) -> Self

Some(x) becomes x’s value; None becomes Value::Null.

Source§

impl From<String> for Value

Source§

fn from(v: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Value>> for Value

Source§

fn from(v: Vec<Value>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Value

Source§

fn from(v: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(v: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 Serialize for Value

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> 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.