#tuple #serialization #convert #api-bindings

no-std tuco

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.

2 releases

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

#788 in Data structures

Download history 209/week @ 2024-11-21 41/week @ 2024-11-28 19/week @ 2024-12-05 6/week @ 2024-12-12

247 downloads per month

MIT license

11KB

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 }

Dependencies

~210–650KB
~15K SLoC