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}");
|
||||
}
|
Reference in New Issue
Block a user