Crates.io | tuco |
lib.rs | tuco |
version | |
source | src |
created_at | 2024-11-24 20:41:16.303259+00 |
updated_at | 2025-02-20 19:13:06.631679+00 |
description | Tuco can automatically generate tuple representations of simple types. This is helpful if you want to create an API using plain types. Or if you want to provide an easy way to convert between types. |
homepage | |
repository | https://gitlab.com/johannesgotlen/tuco |
max_upload_size | |
id | 1459571 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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
SupportTuco is no_std
compatible, meaning it can be used in embedded environments and other contexts where the Rust standard library is unavailable.
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 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));
(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
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.
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
See tuco-derive/tests