initial laptop commit
This commit is contained in:
63
examples/access-macro.rs
Normal file
63
examples/access-macro.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
macro_rules! access {
|
||||
($instance: expr, $trait: path, $with: expr) => {
|
||||
{
|
||||
fn get_thing<T: $trait>(instance: &T) {
|
||||
$with(<T as $trait>::Type)
|
||||
}
|
||||
|
||||
get_thing($instance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Cat;
|
||||
struct Dog;
|
||||
|
||||
trait Animal {
|
||||
type Type;
|
||||
}
|
||||
|
||||
impl Animal for Cat {
|
||||
type Type = u32;
|
||||
}
|
||||
|
||||
impl Animal for Dog {
|
||||
type Type = i64;
|
||||
}
|
||||
|
||||
trait MaxAndMin {
|
||||
type Type;
|
||||
const MAXIMUM: Self::Type;
|
||||
const MINIMUM: Self::Type;
|
||||
}
|
||||
|
||||
impl MaxAndMin for u32 {
|
||||
type Type = u32;
|
||||
const MAXIMUM: Self::Type = u32::MAX;
|
||||
const MINIMUM: Self::Type = u32::MIN;
|
||||
}
|
||||
|
||||
impl MaxAndMin for i64 {
|
||||
type Type = i64;
|
||||
const MAXIMUM: Self::Type = i64::MAX;
|
||||
const MINIMUM: Self::Type = i64::MIN;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let katniss = Cat;
|
||||
let lola = Dog;
|
||||
|
||||
fn get_min<T: Animal>(instance: &T) -> <<T as Animal>::Type as MaxAndMin>::Type where T::Type: MaxAndMin {
|
||||
<T::Type as MaxAndMin>::MINIMUM
|
||||
}
|
||||
|
||||
let katniss_max = get_min(&katniss);
|
||||
let lola_max = get_min(&lola);
|
||||
|
||||
println!("{katniss_max}");
|
||||
println!("{lola_max}");
|
||||
|
||||
// let max = access!(&katniss, Animal, ::MAX);
|
||||
|
||||
// println!("{legs}");
|
||||
}
|
78
examples/relate-macro.rs
Normal file
78
examples/relate-macro.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
struct Katniss;
|
||||
struct Chiko;
|
||||
struct Lola;
|
||||
|
||||
struct Cat;
|
||||
struct Dog;
|
||||
|
||||
struct Jacob;
|
||||
|
||||
macro_rules! relate {
|
||||
($in: ty, $out: ty, $forward: ty, $backward: ty) => {
|
||||
todo!();
|
||||
};
|
||||
}
|
||||
|
||||
relate!(Jacob, Katniss, Loves, IsLovedBy);
|
||||
|
||||
|
||||
// relate_many!(Cat, (Katniss, Chiko), HasMembers, Species);
|
||||
// relate_many!(Dog, (Lola,), HasMembers, Species);
|
||||
|
||||
|
||||
// relate!();
|
||||
|
||||
// relate_bidirectional!();
|
||||
|
||||
// relate_many_bidirectional!();
|
||||
|
||||
// relate_many!(Lola, (Katniss, Chiko), );
|
||||
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
#[rustfmt::skip]
|
||||
macro_rules! all_the_tuples {
|
||||
($name:ident) => {
|
||||
$name!([], T1);
|
||||
$name!([T1], T2);
|
||||
$name!([T1, T2], T3);
|
||||
$name!([T1, T2, T3], T4);
|
||||
$name!([T1, T2, T3, T4], T5);
|
||||
$name!([T1, T2, T3, T4, T5], T6);
|
||||
$name!([T1, T2, T3, T4, T5, T6], T7);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7], T8);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8], T9);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8, T9], T10);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], T11);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11], T12);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12], T13);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13], T14);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14], T15);
|
||||
$name!([T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15], T16);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! all_the_tuples_no_last_special_case {
|
||||
($name:ident) => {
|
||||
$name!(T1);
|
||||
$name!(T1, T2);
|
||||
$name!(T1, T2, T3);
|
||||
$name!(T1, T2, T3, T4);
|
||||
$name!(T1, T2, T3, T4, T5);
|
||||
$name!(T1, T2, T3, T4, T5, T6);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
|
||||
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user