From 483ba1105c36ea9bbac7be346e566d66c72bf14c Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 23 Apr 2022 23:48:03 +0200 Subject: [PATCH] add comments --- src/lib.rs | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 498d02f..3e282ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>), + B(Box>), + // 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) {}