]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/regions-early-bound-lifetime-in-assoc-fn.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / test / run-pass / regions-early-bound-lifetime-in-assoc-fn.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that we are able to compile calls to associated fns like
12 // `decode()` where the bound on the `Self` parameter references a
13 // lifetime parameter of the trait. This example indicates why trait
14 // lifetime parameters must be early bound in the type of the
15 // associated item.
16
17 use std::marker;
18
19 pub enum Value<'v> {
20     A(&'v str),
21     B,
22 }
23
24 pub trait Decoder<'v> {
25     fn read(&mut self) -> Value<'v>;
26 }
27
28 pub trait Decodable<'v, D: Decoder<'v>>
29     : marker::PhantomFn<(), &'v int>
30 {
31     fn decode(d: &mut D) -> Self;
32 }
33
34 impl<'v, D: Decoder<'v>> Decodable<'v, D> for () {
35     fn decode(d: &mut D) -> () {
36         match d.read() {
37             Value::A(..) => (),
38             Value::B => Decodable::decode(d),
39         }
40     }
41 }
42
43 pub fn main() { }