4 releases

new 0.1.3 Feb 20, 2025
0.1.2 Feb 19, 2025
0.1.1 Nov 24, 2024
0.1.0 Nov 24, 2024

#148 in #tuple

Download history 223/week @ 2024-11-22 47/week @ 2024-11-29 16/week @ 2024-12-06 3/week @ 2024-12-13

301 downloads per month
Used in tuco

MIT license

18KB
235 lines

Tuco (crate)

The main entry point for using Tuco. This crate re-exports:

  • tuco-core – Defines the Tuco trait and core conversion logic.
  • tuco-derive – Provides a procedural macro to automatically implement Tuco for structs.

Simply add tuco to your Cargo.toml, and both dependencies will be available.

no_std Support

Tuco is no_std compatible, meaning it can be used in embedded environments and other contexts where the Rust standard library is unavailable.

Tuco-Core (crate)

Tuco enables type conversions based on data equivalence rather than nominal identity. Since tuples are unnamed and only their positional structure matters, any tuple can be converted into a struct as long as the field types match in order. This follows the principle of structural subtyping, where a type is compatible if it satisfies a given structure, even if the names differ.

The Tuco trait, defined in the crate tuco-core, includes an associated type type Tuple, which represents a structurally equivalent tuple for the implementing type.

pub trait Tuco {
    type Tuple;
    fn into_tuple(self) -> Self::Tuple;
    fn from_tuple(tuple: Self::Tuple) -> Self;
    fn from_tuco<T: Tuco<Tuple = Self::Tuple>>(value: T) -> Self;
}

Tuco-Derive (crate)

Tuco-Derive may be used to automatically generate a Tuco trait implementation for your type. If inner fields implement Tuco they should be marked with #[tuco] to enable nested types.

    #[derive(Tuco)]
    struct A(bool);

    #[derive(Tuco)]
    struct B {
        x: i32,
        #[tuco]
        y: A,
        z: bool,
    }

    assert_type_eq_all!(<B as Tuco>::Tuple, (i32, bool, bool));

Single Element Tuple Reduction

(T,) -> T

The Rust language features single element tuples (T,). In order to make Tuco-Derive ergonomic, these kinds of tuples are not generated. Instead single element tuples are converted into its element type T

Background

Type conversions for structs can be tedious to write, often leading to repetitive and error-prone boilerplate code. In many cases, converting between types requires implementing explicit conversion traits for each type pair, making maintenance difficult as the codebase grows.

This crate aims to simplify type conversions by utilizing a universally accessible intermediate representation: tuples. By converting types into their equivalent positional tuple representations, it enables seamless transformation between structurally compatible types without requiring direct relationships between them.

By leveraging tuples as a common denominator, this crate allows types to be converted to and from a standard format, making interoperability between different types much easier while maintaining Rust’s strong type safety guarantees.

Todo

  • Support structs with named fields
  • Support structs with unnamed fields
  • Support nested tuco
  • Support structs with generic types
  • Reduce single element structs into their inner type, instead of using single element tuples.
  • Tests
  • Documentation
  • Improved derive macro compiler errors

Examples

See tuco-derive/tests

Dependencies

~250–710KB
~17K SLoC