Enum valuable::TupleDef[][src]

#[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

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

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

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());

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());

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.