mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-16 18:35:02 +01:00
add comments
This commit is contained in:
parent
7d8e5a2397
commit
483ba1105c
1 changed files with 29 additions and 18 deletions
47
src/lib.rs
47
src/lib.rs
|
|
@ -1,33 +1,44 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
// a fancy trait
|
||||||
pub trait CoolTrait {}
|
pub trait CoolTrait {}
|
||||||
|
|
||||||
impl<'a, D: CoolTrait + ?Sized> CoolTrait for &'a D {}
|
// implement that trait for all reference to someone implementing the trait
|
||||||
|
impl<'a, D: CoolTrait> CoolTrait for &'a D {}
|
||||||
pub fn foo(parent: Parent<'_>) {
|
|
||||||
requires_parent_fulfill_cool_trait(parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn requires_parent_fulfill_cool_trait(_: impl CoolTrait) {}
|
|
||||||
|
|
||||||
|
// a type with a lifetime that contains two other types
|
||||||
pub enum Parent<'a> {
|
pub enum Parent<'a> {
|
||||||
A(&'a A<'a>),
|
A(Box<A<'a>>),
|
||||||
B(&'a B<'a>),
|
B(Box<B<'a>>),
|
||||||
}
|
// We need a lifetime or else everything is fine
|
||||||
|
Boo(PhantomData<&'a ()>)
|
||||||
impl<'a> CoolTrait for Parent<'a>
|
|
||||||
where
|
|
||||||
&'a A<'a>: CoolTrait,
|
|
||||||
&'a B<'a>: CoolTrait,
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// those two types that in turn contain the parent type recursively
|
||||||
pub struct A<'a> {
|
pub struct A<'a> {
|
||||||
parent: Parent<'a>,
|
parent: Parent<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CoolTrait for A<'a> where Parent<'a>: CoolTrait {}
|
|
||||||
|
|
||||||
pub struct B<'a> {
|
pub struct B<'a> {
|
||||||
parent: Parent<'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 {}
|
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) {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue