Как исправить «противоречивые требования к сроку службы» - PullRequest
0 голосов
/ 20 сентября 2019

Я пытаюсь реализовать Iterator для JSON-подобной структуры.Руст жалуется на то, что не может определить соответствующую продолжительность жизни.Я не понимаю проблему, и был бы признателен за объяснение.Мне нужно добавить еще несколько деталей, но я не совсем понимаю проблему.

--> src/ipld.rs:92:44
   |
92 |             if let Some(iter) = self.stack.last() {
   |                                            ^^^^
   |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 87:6...
  --> src/ipld.rs:87:6
   |
87 | impl<'a> Iterator for IpldIter<'a> {
   |      ^^
   = note: ...so that the types are compatible:
           expected &[std::boxed::Box<dyn std::iter::Iterator<Item = &ipld::Ipld>>]
              found &[std::boxed::Box<(dyn std::iter::Iterator<Item = &'a ipld::Ipld> + 'static)>]
   = note: but, the lifetime must be valid for the static lifetime...
   = note: ...so that the expression is assignable:
           expected std::boxed::Box<(dyn std::iter::Iterator<Item = &'a ipld::Ipld> + 'static)>
              found std::boxed::Box<dyn std::iter::Iterator<Item = &ipld::Ipld>>
pub enum Ipld {
    List(Vec<Ipld>),
    Bool(bool),
}

pub struct IpldIter<'a> {
    stack: Vec<Box<dyn Iterator<Item = &'a Ipld>>>,
}

impl<'a> IpldIter<'a> {
    fn new(ipld: &'a Ipld) -> Self {
        let iter = vec![ipld].into_iter();
        Self {
            stack: vec![Box::new(iter)],
        }
    }
}

impl<'a> Iterator for IpldIter<'a> {
    type Item = &'a Ipld;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(iter) = self.stack.last() {
                if let Some(ipld) = iter.next() {
                    match ipld {
                        Ipld::List(list) => {
                            self.stack.push(Box::new(list.iter()));
                        }
                        _ => {}
                    }
                    return Some(ipld);
                } else {
                    self.stack.pop();
                }
            } else {
                return None;
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...