Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::marker::PhantomData;
- struct Qux<'a> {
- _used_lifetime: PhantomData<&'a str>,
- }
- struct Bar<'a, S> {
- phantom: PhantomData<S>,
- qux: Qux<'a>,
- }
- impl<'a, S> Bar<'a, S>
- where
- // How to create lifetime bound higher-rank trait bounds?
- S: for<'b, 'c> FooMaker<'b, 'c>,
- {
- fn new() -> Self {
- Bar {
- phantom: PhantomData,
- qux: Qux {
- _used_lifetime: PhantomData,
- }
- }
- }
- fn foo_method(&'a mut self) {
- S::Foo::new(&mut self.qux).do_stuff();
- S::Foo::new(&mut self.qux).do_stuff();
- }
- }
- trait FooMaker<'a, 'b: 'a> {
- type Foo: Foo<'a, 'b>;
- }
- trait Foo<'a, 'b: 'a> {
- fn new(qux: &'a mut Qux<'b>) -> Self;
- fn do_stuff(&mut self);
- }
- struct FooAMaker {}
- impl<'a, 'b: 'a> FooMaker<'a, 'b> for FooAMaker {
- type Foo = FooA<'a, 'b>;
- }
- struct FooA<'a, 'b: 'a> {
- qux: &'a mut Qux<'b>,
- }
- impl<'a, 'b> Foo<'a, 'b> for FooA<'a, 'b> {
- fn new(qux: &'a mut Qux<'b>) -> FooA<'a, 'b> {
- FooA {
- qux,
- }
- }
- fn do_stuff(&mut self) {
- }
- }
- fn main() {
- let mut v = Vec::<u8>::new();
- Bar::<FooAMaker>::new(&mut v).foo_method();
- }
Add Comment
Please, Sign In to add comment