Skip to main content

structural/structural_aliases/
tuple_traits.rs

1/*!
2Contains structural aliases for tuples,
3with shared,mutable,and by value access to every field of the tuple.
4
5`Tuple*` traits can be used with any tuple at least as large as the size indicated
6by the trait.
7You can use `Tuple3` with any tuple type starting with `(A,B,C`.
8Eg:`(A,B,C)`,`(A,B,C,D)`,`(A,B,C,D,E)`,etcetera.
9
10
11# `Tuple*` Example
12
13Demonstrates that you can use the `Tuple*` trait with structs.
14
15Note that the `Tuple*Variant` traits require the fields to have mutable and
16by-value accessor trait impls,
17satisfying the [IntoFieldMut trait](../../field/trait.IntoFieldMut.html)
18
19```
20use structural::structural_aliases::tuple_traits::Tuple4;
21use structural::{StructuralExt,Structural, fp};
22
23fn sum_tuple_4(tuple: impl Tuple4<u8, u16, u32, u64>) -> u64 {
24    let (a, b, c, d) = tuple.fields(fp!(0, 1, 2, 3));
25    *a as u64 + *b as u64 + *c as u64 + *d
26}
27
28assert_eq!(sum_tuple_4((3, 5, 8, 13)), 29);
29
30assert_eq!(sum_tuple_4((1, 2, 4, 8, "what?")), 15);
31
32assert_eq!(sum_tuple_4((1, 3, 9, 27, "Noooooo", "Impossible!")), 40);
33
34assert_eq!(sum_tuple_4(MyTuple4(1, 1, 1, 1)), 4);
35
36assert_eq!(sum_tuple_4(MyTuple5(2, 2, 2, 2, "foo".into())), 8);
37
38
39#[derive(Structural)]
40struct MyTuple4(pub u8,pub u16,pub u32,pub u64);
41
42#[derive(Structural)]
43struct MyTuple5(pub u8,pub u16,pub u32,pub u64, String);
44
45```
46
47# `Tuple*Variant` Example
48
49Demonstrates that you can use the `Tuple*Variant` trait with enums.
50
51Note that the `Tuple*Variant` traits require the fields to have mutable and
52by-value accessor traits,
53satisfying the ([IntoVariantFieldMut trait](../../field/trait.IntoVariantFieldMut.html))
54
55```
56use structural::structural_aliases::Tuple2Variant;
57use structural::{StructuralExt,Structural,TS,fp};
58
59use std::cmp::{Ordering,PartialEq};
60use std::fmt::Debug;
61
62fn first_2<This,T>(mut foo: This, mut not_foo: This)
63where
64    This: Tuple2Variant<&'static str, T, TS!(Foo)> + Copy,
65    T: Debug + From<u8> + PartialEq,
66{
67    {
68        assert_eq!( foo.fields(fp!(::Foo=>0,1)), Some(( &"heh", &T::from(88) )) );
69
70        assert_eq!(
71            foo.fields_mut(fp!(::Foo=>0,1)),
72            Some(( &mut "heh", &mut T::from(88) )),
73        );
74
75        assert_eq!( foo.into_field(fp!(::Foo.0)), Some("heh") );
76        assert_eq!( foo.into_field(fp!(::Foo.1)), Some(T::from(88)) );
77
78        assert_eq!( foo.is_variant(fp!(Foo)), true );
79    }
80    {
81        assert_eq!( not_foo.fields(fp!(::Foo=>0,1)), None );
82
83        assert_eq!( not_foo.fields_mut(fp!(::Foo=>0,1)), None );
84
85        assert_eq!( not_foo.into_field(fp!(::Foo.0)), None );
86        assert_eq!( not_foo.into_field(fp!(::Foo.1)), None );
87
88        assert_eq!( not_foo.is_variant(fp!(Foo)), false );
89    }
90}
91
92first_2(
93    Enum::Foo("heh", 88_u8, Ordering::Less),
94    Enum::Bar,
95);
96first_2(
97    OtherEnum::Foo("heh", 88_u64, false),
98    OtherEnum::Bar,
99);
100
101#[derive(Structural,Copy,Clone)]
102# #[struc(no_trait)]
103enum Enum<T>{
104    Foo(&'static str,T,Ordering),
105    Bar,
106}
107
108#[derive(Structural,Copy,Clone)]
109# #[struc(no_trait)]
110enum OtherEnum<T>{
111    Foo(&'static str,T,bool),
112    Bar,
113}
114
115```
116
117
118*/
119
120pub use crate::field::tuple_impls::*;