Enum valuable::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

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.

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!(),
};
§

Dynamic

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.

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)))
    }
}

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