add comments

This commit is contained in:
nora 2022-04-23 23:48:03 +02:00
parent 7d8e5a2397
commit 483ba1105c

View file

@ -1,33 +1,44 @@
use std::marker::PhantomData;
// a fancy trait
pub trait CoolTrait {}
impl<'a, D: CoolTrait + ?Sized> CoolTrait for &'a D {}
pub fn foo(parent: Parent<'_>) {
requires_parent_fulfill_cool_trait(parent);
}
pub fn requires_parent_fulfill_cool_trait(_: impl CoolTrait) {}
// implement that trait for all reference to someone implementing the trait
impl<'a, D: CoolTrait> CoolTrait for &'a D {}
// a type with a lifetime that contains two other types
pub enum Parent<'a> {
A(&'a A<'a>),
B(&'a B<'a>),
}
impl<'a> CoolTrait for Parent<'a>
where
&'a A<'a>: CoolTrait,
&'a B<'a>: CoolTrait,
{
A(Box<A<'a>>),
B(Box<B<'a>>),
// We need a lifetime or else everything is fine
Boo(PhantomData<&'a ()>)
}
// those two types that in turn contain the parent type recursively
pub struct A<'a> {
parent: Parent<'a>,
}
impl<'a> CoolTrait for A<'a> where Parent<'a>: CoolTrait {}
pub struct B<'a> {
parent: Parent<'a>,
}
// implement CoolTrait only when the two types themselves implement it
impl<'a> CoolTrait for Parent<'a>
where
A<'a>: CoolTrait,
B<'a>: CoolTrait,
{
}
// implement CoolTrait for the two types only when the Parent also implements it
// oh no! a cycle!
impl<'a> CoolTrait for A<'a> where Parent<'a>: CoolTrait {}
impl<'a> CoolTrait for B<'a> where Parent<'a>: CoolTrait {}
// now test whether CoolTrait is implemented for
pub fn foo(parent: Parent<'_>) {
requires_parent_fulfill_cool_trait(parent);
}
pub fn requires_parent_fulfill_cool_trait(_: impl CoolTrait) {}