valuable

Enum TupleDef

Source
#[non_exhaustive]
pub enum TupleDef { Static { fields: usize, }, Dynamic { fields: (usize, Option<usize>), }, }
Expand description

The number of fields and other tuple-level information.

Returned by Tuplable::definition(), TupleDef provides the caller with information about the tuple’s definition.

This includes the number of fields contained by the tuple.

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

Static

The tuple is statically-defined, all fields are known ahead of time.

Static tuple implementations are provided by the crate.

§Examples

A statically defined tuple.

use valuable::{Tuplable, TupleDef};

let tuple = (123, "hello");

match tuple.definition() {
    TupleDef::Static { fields, .. } => {
        assert_eq!(2, fields);
    }
    _ => unreachable!(),
};

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
§fields: usize

The number of fields contained by the tuple.

§

Dynamic

The tuple is dynamically-defined, not all fields are known ahead of time.

§Examples

use valuable::{Tuplable, TupleDef, Valuable, Value, Visit};

struct MyTuple;

impl Valuable for MyTuple {
    fn as_value(&self) -> Value<'_> {
        Value::Tuplable(self)
    }

    fn visit(&self, visit: &mut dyn Visit) {
        visit.visit_unnamed_fields(&[Value::I32(123)]);
        visit.visit_unnamed_fields(&[Value::String("hello world")]);
    }
}

impl Tuplable for MyTuple {
    fn definition(&self) -> TupleDef {
        TupleDef::new_dynamic((1, Some(3)))
    }
}

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
§fields: (usize, Option<usize>)

Returns the bounds on the number of tuple fields.

Specifically, the first element is the lower bound, and the second element is the upper bound.

Implementations§

Source§

impl TupleDef

Source

pub const fn new_static(fields: usize) -> TupleDef

Create a new TupleDef::Static instance

This should be used when the tuple’s fields are fixed and known ahead of time.

§Examples
use valuable::TupleDef;

let def = TupleDef::new_static(2);
Source

pub const fn new_dynamic(fields: (usize, Option<usize>)) -> TupleDef

Create a new TupleDef::Dynamic instance.

This is used when the tuple’s fields may vary at runtime.

§Examples
use valuable::TupleDef;

let def = TupleDef::new_dynamic((2, Some(10)));
Source

pub fn is_unit(&self) -> bool

Returns true if self represents the unit tuple.

§Examples

With the unit tuple

use valuable::Tuplable;

let tuple: &dyn Tuplable = &();
assert!(tuple.definition().is_unit());

When not the unit tuple.

use valuable::Tuplable;

let tuple: &dyn Tuplable = &(123,456);
assert!(!tuple.definition().is_unit());
Source

pub fn is_static(&self) -> bool

Returns true if the tuple is statically defined.

§Examples

With a static tuple

use valuable::TupleDef;

let def = TupleDef::new_static(2);
assert!(def.is_static());

With a dynamic tuple

use valuable::TupleDef;

let def = TupleDef::new_dynamic((2, None));
assert!(!def.is_static());
Source

pub fn is_dynamic(&self) -> bool

Returns true if the tuple is dynamically defined.

§Examples

With a static tuple

use valuable::TupleDef;

let def = TupleDef::new_static(2);
assert!(!def.is_dynamic());

With a dynamic tuple

use valuable::TupleDef;

let def = TupleDef::new_dynamic((2, None));
assert!(def.is_dynamic());

Trait Implementations§

Source§

impl Debug for TupleDef

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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