Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

rust - Why can't this higher kinded lifetime associated type trait bound be satisfied?

trait A<'self_>: 'self_ {
    type I;
}
trait AMut
where
    Self: for<'self_> A<'self_>,
    for<'self_> <Self as A<'self_>>::I: 'static,
{
    fn mutate_self(&mut self);
}

fn foo<X>(x: &mut X)
where
    X: 'static + for<'a> A<'a> + AMut,
    for<'a> <X as A<'a>>::I: 'static,
{
    x.mutate_self();
}

Playground

This errors out with

error[E0280]: the requirement `for<'self_> <Self as A<'self_>>::I: 'static` is not satisfied
  --> src/lib.rs:4:1
   |
4  |   trait AMut
   |   ^     ---- required by a bound in this
   |  _|
   | |
5  | | where
6  | |     Self: for<'self_> A<'self_>,
7  | |     for<'self_> <Self as A<'self_>>::I: 'static,
   | |                                         ------- required by this bound in `AMut`
8  | | {
9  | |     fn mutate_self(&mut self);
10 | | }
   | |_^

error[E0280]: the requirement `for<'self_> <X as A<'self_>>::I: 'static` is not satisfied
  --> src/lib.rs:14:34
   |
4  | trait AMut
   |       ---- required by a bound in this
...
7  |     for<'self_> <Self as A<'self_>>::I: 'static,
   |                                         ------- required by this bound in `AMut`
...
14 |     X: 'static + for<'a> A<'a> + AMut,
   |                                  ^^^^

I would've thought that the bound on line 15 would satisfy the bound on line 7. What am I missing?

question from:https://stackoverflow.com/questions/34114048/why-cant-this-higher-kinded-lifetime-associated-type-trait-bound-be-satisfied

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried like this?

trait A<'self_>: 'self_ {
    type I;
}
trait AMut<'a>
where
    Self: A<'a>,
    <Self as A<'a>>::I: 'static,
{
    fn mutate_self(&mut self);
}

fn foo<X>(x: &mut X)
where
    X: 'static + for<'a> A<'a> + for<'a> AMut<'a>,
    for<'a> <X as A<'a>>::I: 'static,
{
    x.mutate_self();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...