How can I explicitly specify a lifetime when implementing a trait?

别说谁变了你拦得住时间么 提交于 2021-01-28 04:42:39

问题


Given the implementation below, where essentially I have some collection of items that can be looked up via either a i32 id field or a string field. To be able to use either interchangeably, a trait "IntoKey" is used, and a match dispatches to the appropriate lookup map; this all works fine for my definition of get within the MapCollection impl:

use std::collections::HashMap;
use std::ops::Index;

enum Key<'a> {
    I32Key(&'a i32),
    StringKey(&'a String),
}

trait IntoKey<'a> {
    fn into_key(&'a self) -> Key<'a>;
}

impl<'a> IntoKey<'a> for i32 {
    fn into_key(&'a self) -> Key<'a> { Key::I32Key(self) }
}

impl<'a> IntoKey<'a> for String {
    fn into_key(&'a self) -> Key<'a> { Key::StringKey(self) }
}

#[derive(Debug)]
struct Bar {
    i: i32,
    n: String,
}

struct MapCollection
{
    items: Vec<Bar>,
    id_map: HashMap<i32, usize>,
    name_map: HashMap<String, usize>,
}

impl MapCollection {
    fn new(items: Vec<Bar>) -> MapCollection {
        let mut is = HashMap::new();
        let mut ns = HashMap::new();
        for (idx, item) in items.iter().enumerate() {
            is.insert(item.i, idx);
            ns.insert(item.n.clone(), idx);
        }
        MapCollection {
            items: items,
            id_map: is,
            name_map: ns,
        }
    }

    fn get<'a, K>(&self, key: &'a K) -> Option<&Bar>
        where K: IntoKey<'a> //'
    {
        match key.into_key() {
            Key::I32Key(i)    => self.id_map.get(i).and_then(|idx|     self.items.get(*idx)),
            Key::StringKey(s) => self.name_map.get(s).and_then(|idx|     self.items.get(*idx)),
        }
    }
}

fn main() {
    let bars = vec![Bar { i:1, n:"foo".to_string() }, Bar { i:2, n:"far".to_string() }];
    let map = MapCollection::new(bars);
    if let Some(bar) = map.get(&1) {
        println!("{:?}", bar);
    }
    if map.get(&3).is_none() {
        println!("no item numbered 3");
    }
    if let Some(bar) = map.get(&"far".to_string()) {
        println!("{:?}", bar);
    }
    if map.get(&"baz".to_string()).is_none() {
        println!("no item named baz");
    }
}

However, if I then want to implement std::ops::Index for this struct, if I attempt to do the below:

impl<'a, K> Index<K> for MapCollection
where K: IntoKey<'a> {
    type Output = Bar;

    fn index<'b>(&'b self, k: &K) -> &'b Bar {
        self.get(k).expect("no element")
    }
}

I hit a compiler error:

src/main.rs:70:18: 70:19 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
src/main.rs:70         self.get(k).expect("no element")
                            ^
src/main.rs:69:5: 71:6 help: consider using an explicit lifetime parameter as shown: fn index<'b>(&'b self, k: &'a K) -> &'b Bar
src/main.rs:69     fn index<'b>(&'b self, k: &K) -> &'b Bar {
src/main.rs:70         self.get(k).expect("no element")
src/main.rs:71     }

I can find no way to specify a distinct lifetime here; following the compiler's recommendation is not permitted as it changes the function signature and no longer matches the trait, and anything else I try fails to satisfy the lifetime specification.

I understand that I can implement the trait for each case (i32, String) separately instead of trying to implement it once for IntoKey, but I am more generally trying to understand lifetimes and appropriate usage. Essentially:

  • Is there actually an issue the compiler is preventing? Is there something unsound about this approach?
  • Am I specifying my lifetimes incorrectly? To me, the lifetime 'a in Key/IntoKey is dictating that the reference need only live long enough to do the lookup; the lifetime 'b associated with the index fn is stating that the reference resulting from the lookup will live as long as the containing MapCollection.
  • Or am I simply not utilizing the correct syntax to specify the needed information?

(using rustc 1.0.0-nightly (b63cee4a1 2015-02-14 17:01:11 +0000))


回答1:


Do you intend on implementing IntoKey on struct's that are going to store references of lifetime 'a? If not, you can change your trait and its implementations to:

trait IntoKey {
    fn into_key<'a>(&'a self) -> Key<'a>;
}

This is the generally recommended definition style, if you can use it. If you can't...

Let's look at this smaller reproduction:

use std::collections::HashMap;
use std::ops::Index;

struct Key<'a>(&'a u8);

trait IntoKey<'a> { //'
    fn into_key(&'a self) -> Key<'a>;
}

struct MapCollection;

impl MapCollection {
    fn get<'a, K>(&self, key: &'a K) -> &u8
        where K: IntoKey<'a> //'
    {
        unimplemented!()
    }
}

impl<'a, K> Index<K> for MapCollection //'
    where K: IntoKey<'a> //'
{
    type Output = u8;

    fn index<'b>(&'b self, k: &K) -> &'b u8 { //'
        self.get(k)
    }
}

fn main() {
}

The problem lies in get:

fn get<'a, K>(&self, key: &'a K) -> &u8
    where K: IntoKey<'a>

Here, we are taking a reference to K that must live as long as the Key we get out of it. However, the Index trait doesn't guarantee that:

fn index<'b>(&'b self, k: &K) -> &'b u8

You can fix this by simply giving a fresh lifetime to key:

fn get<'a, 'b, K>(&self, key: &'b K) -> &u8
    where K: IntoKey<'a>

Or more succinctly:

fn get<'a, K>(&self, key: &K) -> &u8
    where K: IntoKey<'a>


来源:https://stackoverflow.com/questions/28574458/how-can-i-explicitly-specify-a-lifetime-when-implementing-a-trait

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!