#tuple #serialization #traits #convert #api-bindings

no-std tuco-core

Contains the Tuco trait, used by the Tuco crate

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

#2646 in Data structures


Used in 2 crates

MIT license

5KB

Tuco

Tuco can automatically generate tuple representations of simple types. This is helpful if you want your API to expose convertible types. Down converting a struct to a tuple erases the struct field names resulting in an object which is up convertible to a struct with the similar field composition.

Using #[derive(Tuco)] Will automatically derive Tuco and generate a tuple representation of your struct and assign it to the associated type Tuco::Tuple

Tuco can be used in places where data serialization is used with some differences:

  • Conversion operations are validated at compile time.
  • Since tuples are unnamed, any matching tuple shape may be used to construct your type. No further integrity checks are applied at this time.
  • Tuco does not automatically serialize your types to string data.

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
  • Compiler errors

Example

    fn tuco_equivalent() {
        #[derive(Tuco)]
        struct MyStruct {
            a: i32,
            b: String,
            c: bool,
        }

        #[derive(Tuco)]
        struct MyOtherStruct {
            x: i32,
            y: String,
            z: bool,
        }
    }
    #[derive(Tuco)]
    struct MyNestedStruct {
        x: i32,
        #[tuco]
        y: MyStruct,
        z: bool,
    }

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

    fn nested_struct() {
        let my_nested_struct = MyNestedStruct {
            x: 10,
            y: MyStruct {
                a: 1,
                b: "qwe".to_string(),
                c: true,
            },
            z: false,
        };

        println!("{:?}", my_nested_struct);

        let my_other_struct = MyOtherNestedStruct::from_tuco(my_nested_struct);

        println!("{:?}", my_other_struct);
    } 

Output:

    MyNestedStruct { x: 10, y: MyStruct { a: 1, b: "qwe", c: true }, z: false }
    MyOtherNestedStruct { x: 10, y: MyOtherStruct { x: 1, y: "qwe", z: true }, z: false }

No runtime deps