Seoul-Rs
- derive trait Isomorphism for Enum data
- derive trait Tuplike for Struct and Enum data
- derive trait Reflica for Struct and Enum data
- derive trait IntoWrap for Enum data
Trait Isomorphism
-
for enum data type. Convenient transformation of enum values with derive macro.
-
Basic methods:
fn title(&self) -> String;fn list() -> Vec<Self>;
-
The derive macro also implements
Into<T>forSelfand&SelfFrom<T>andFrom<&T>forSelf(whenDefaultis implemented and "has_default" syntax is given)
derive syntax and fallback
- When title is not given at variant level, the variant's name (Ident) will be used as titile.
- When list is not given at the top level attribute, list of each variant's default format will be returned.
- When into value is not given at variant level, the into type(T)'s default value will be used in
<Into<T>>trait. - When the type implements
Defaultand has_default is given at top level attribute, theFrom<T>andFrom<&T>will be implemented for each given into types(T).
Examples
use Isomorphism;
// `list()`
let list = ABClist;
assert_eq!;
let a: ABC = ABCA;
let b = ABCB;
let c = ABCC;
// `title()`
assert_eq!;
assert_eq!;
assert_eq!;
// `Into<T>` for `&Self` and `Self`
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// list
assert_eq!;
// Into
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// From
assert_eq!;
assert_eq!;
// fallback to default value of `CD`
assert_eq!;
Trait Tuplike
-
for struct data type. Transform a struct data into tuple format
AB { a: 0, b: 10 }<=>(0, 10)
-
Using derive macro, you can implement
- trait
From<T>forSelf - trait
Into<T>forSelf - trait
Into<R>for&Self - Hereby,
Tis a tuple format of the struct's fields, - and the
Ris a referenced tuple format of them.
- trait
-
for the enum data type, Only
From<T>trait will be implemted for each variant.
Example
use Tuplike;
let tuple_: = ;
let ab_: AB = AB ;
let ab_into: = ab_.clone.into;
let tuple_into: AB = tuple_.clone.into;
assert_eq!;
assert_eq!;
let _ab_ref_into: = .into;
// for enum, just `From<T>` will be implemented for each variant.
let _ = ABCA;
let b1: = ;
let b2: ABC = ABCB;
let b1_: ABC = b1.clone.into;
assert_eq!;
let c1: = ;
let c2: ABC = ABCC ;
let c1_: ABC = c1.clone.into;
assert_eq!;
Trait Reflica
-
Declare a borrowed fields' data type ("reflica") from an original struct/enum data type, and implement Into trait to the reflica.
-
concept of reflica:
struct AB { a: u8, b: String }->- declare
struct RefAB<'a> { a: &'a u8, b: &'a String } - implement
Into<RefAB<'a>> for &'a AB
- declare
enum AB { A, B { a: u8, b: String } }->- declare
enum RefAB<'a> { A, B { a: &'a u8, b: &'a String } } - implement
Into<RefAB<'a>> for &'a AB
- declare
Example
use Reflica;
// struct
// attribute for derive implementation for the reflica
// RefAB delcared
let _: = RefAB ;
// check Into<RefAB> for &AB
let x = AB ;
let _: = .into;
// check derive `Ord`
let a: = RefAB ;
let b: = RefAB ;
let c: = RefAB ;
let x = vec!;
let mut y = x.clone;
y.sort;
assert_eq!;
// enum, use prefix other than basic `Ref`
// `prefix` attribute's string value will be used for reflica's prefix, other than the basic prefix `Ref`
// Ref2AB delcared
let _: = A;
let _: = B ;
// check Into<Ref2AB>
let x = ABCB ;
let _: = .into;
let x = ABC::C;
let _: = .into;
Trait IntoWrap
-
Implement
Fromtraits for each variants of an enum type data, where transforming field's data into the enum data when the variant has one unnamed field. -
Thus each variants should not have any redundant type or generic to avoid any conflicting implementations.
-
IntoWrapcan be though as a not-tuple single variable version ofTuplikefor enum's wrapping variants.
Example
use IntoWrap;
let _ = ABCDA;
let _ = ABCDC ;
let b: i32 = 10;
let b: ABCD = b.into;
let d = "string".to_string;
let d: ABCD = d.into;
assert_eq!;
assert_eq!;
let a: = ;
let a: = a.into;
let b: = vec!;
let b: = b.into;
assert_eq!;
assert_eq!;
// This case won't work due to conflicting implementations.
/*#[derive(Debug, Clone, PartialEq, IntoWrap)]
enum AB<X: Clone, Y> where Y: Clone {
A(X),
B(Vec<Y>)
}*/
Dev Log
- ver.0.2.0
- `From<T>` implemented only with `has_default` attribute syntax when `Default` is implemented.
- ver 0.2.1~2
- correct some typos
- ver 0.3.0
- Add `Tuplike`.
- ver 0.3.1
- Add an associated type `Tuple` to the trait `Tuplike`
- ver 0.3.2
- Add `Reflica`
- ver 0.3.3
- On `Tuplike`:
- delete associated type `Tuple` from the trait.
- derive macro now works on the enum type too: but only `From<T>` trait will be implemented for each variant.
- Add `IntoWrap`
- ver 0.3.5
- On `Isomorphism`:
- Reinforced derive macro's generic parsing ability.
- For struct type, the derive macro would only implement the Isormophism trait with each methods of it returning default values.