#tuple #struct #into #convert #recursive

intuple

Convert structs into/from tuples - recursive, ignore fields, distinct traits

1 unstable release

Uses new Rust 2021

0.1.0 Sep 25, 2022

#671 in Rust patterns

42 downloads per month
Used in 2 crates (via querio)

MIT/Apache

11KB

intuple

Convert structs into/from tuples

🐠 add intuple to the dependencies in the Cargo.toml:

[dependencies]
intuple = "0.1.0"

🦀 use/import everything into rust:

use intuple::*;

🦚 multiple possibilities to convert - combine as you wish - whatever fits your use case:

#[derive(Intuple)]
struct Struct {a:u32, b:u32, c:u32}

fn main(){
    // use std traits
        let strct: Struct = (3,2,1).into();
        let tuple = <(u32, u32, u32)>::from(strct);
        // OR
        let strct = Struct::from((3,2,1));
        let tuple: (u32, u32, u32) = strct.into();
    // OR intuple traits
        let strct: Struct = (3,2,1).into_struct();
        let tuple = StructIntuple::from_struct(strct);
        // OR
        let strct = Struct::from_tuple((3,2,1));
        let tuple = strct.into_tuple();
}

Tuple Type

🦊 access the resulting tuple type of a struct easily:

#[derive(Intuple)]
struct Nice {a:u32, b:u32, c:u32}
fn main(){
    // easiest: through {StructName}Intuple
    let tup: NiceIntuple = (3,2,1);
    // is ALWAYS equal to
    let tup: <Nice as IntupleStruct>::Intuple = (3,2,1);
    // is IN THIS CASE equal to
    let tup: (u32, u32, u32) = (3,2,1);
}

Ignoring

🦥 ignore specific fields with #[igno]
🐼 ignored fields need/use Default while converting to a struct

#[derive(Intuple)]
struct Struct {a:u32, #[igno] b:u32, c:u32}
fn main(){
    let strct = Struct::from((2,1));     
    // => {a:2, b:0, c:1}  
    let tuple: (u32, u32) = strct.into();
    // => (2, 1)
}

Recursion

🦊 convert recursively with #[recursive] or #[rcsv]
🐼 recursive fields need to derive Intuple

#[derive(Intuple)]
struct Struct {a:u32, b:u32, c:u32}
#[derive(Intuple)]
struct Recursive {a:u32, #[recursive] b:Struct, c:u32}
fn main(){
    let rcsv: Recursive = (9,(3,2,1),8).into(); 
    // => Recursive{a:9, b:Struct{a:3,b:2,c:1}, c:8}
    let tuple: RecursiveIntuple = rcsv.into(); 
    // => (9,(3,2,1),8)
}

More Information

🦎 Changelog
🐱 GitHub
👾 Discord Server


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~210–610KB
~15K SLoC