【问题标题】:How to implement decorator in Rust?如何在 Rust 中实现装饰器?
【发布时间】:2021-01-23 21:06:34
【问题描述】:

我正在学习 Rust,但我被一个玩具示例困住了。我已经阅读了关于生命周期的文档、post 和一堆关于 Stack Overflow 的问题。我已经花了一个多星期了,但还是卡住了,所以我决定向社区寻求帮助。

我有一个通用特征BookSide,它返回BookIterator(它扩展了通常的Iterator)。我对BookSideBookIterator 都有两个实现:ArrayBookSideCommissionBookSide

  1. 第一个是有状态的。它在引擎盖下有一个Vec
  2. 第二个是无状态的:它包装了其他一些BookSide

我的目标只是编译整个内容。我正在解决问题并遵循编译器的建议。这个过程产生了以下代码。

use std::marker::PhantomData;

fn main() {
    println!("Hello, world!");
}

// traits

pub trait BookIterator<'a>: Iterator<Item=f64> {}

pub trait BookSide<'a> {
    type BookIteratorType: BookIterator<'a>;

    fn book_iterator(&self) -> Self::BookIteratorType;
}

// implementation 1: stateful

pub struct ArrayBookSide {
    quotes: Vec<f64>,
}

pub struct ArrayBookSideIterator<'a> {
    quotes_iter: std::slice::Iter<'a, f64>,
}

impl<'a> BookSide<'a> for ArrayBookSide {
    type BookIteratorType = ArrayBookSideIterator<'a>;

    fn book_iterator(&self) -> Self::BookIteratorType {
        ArrayBookSideIterator { quotes_iter: self.quotes.iter() }
    }
}

impl<'a> Iterator for ArrayBookSideIterator<'a> {
    type Item = f64;

    fn next(&mut self) -> Option<Self::Item> {
        self.quotes_iter.next().map(|&quote| quote)
    }
}

impl<'a> BookIterator<'a> for ArrayBookSideIterator<'a> {}

// implementation 2: delegating

pub struct CommissionBookSide<'a, B>
    where B: BookSide<'a> {
    base_book_side: B,
    multiplier: f64,
    _marker: PhantomData<&'a B>,
}

impl<'a, B> CommissionBookSide<'a, B>
    where B: BookSide<'a> {
    pub fn new(base_book_side: B) -> CommissionBookSide<'a, B> {
        CommissionBookSide { base_book_side, multiplier: 1.1, _marker: PhantomData {} }
    }
}

impl<'a, B> BookSide<'a> for CommissionBookSide<'a, B>
    where B: BookSide<'a> {
    type BookIteratorType = CommissionIterator<'a, B::BookIteratorType>;

    fn book_iterator(&self) -> Self::BookIteratorType {
        CommissionIterator {
            base_iterator: self.base_book_side.book_iterator(),
            multiplier: self.multiplier,
            _marker: PhantomData {},
        }
    }
}

pub struct CommissionIterator<'a, BI>
    where BI: BookIterator<'a> {
    base_iterator: BI,
    multiplier: f64,
    _marker: PhantomData<&'a BI>,
}

impl<'a, BI> Iterator for CommissionIterator<'a, BI>
    where BI: BookIterator<'a> {
    type Item = BI::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.base_iterator.next().map(|quote| quote * self.multiplier)
    }
}

impl<'a, BI> BookIterator<'a> for CommissionIterator<'a, BI>
    where BI: BookIterator<'a> {}

现在我有以下编译错误。

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
  --> src/main.rs:31:58
   |
31 |         ArrayBookSideIterator { quotes_iter: self.quotes.iter() }
   |                                                          ^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 30:5...
  --> src/main.rs:30:5
   |
30 | /     fn book_iterator(&self) -> Self::BookIteratorType {
31 | |         ArrayBookSideIterator { quotes_iter: self.quotes.iter() }
32 | |     }
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:31:46
   |
31 |         ArrayBookSideIterator { quotes_iter: self.quotes.iter() }
   |                                              ^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 27:6...
  --> src/main.rs:27:6
   |
27 | impl<'a> BookSide<'a> for ArrayBookSide {
   |      ^^
note: ...so that the types are compatible
  --> src/main.rs:30:55
   |
30 |       fn book_iterator(&self) -> Self::BookIteratorType {
   |  _______________________________________________________^
31 | |         ArrayBookSideIterator { quotes_iter: self.quotes.iter() }
32 | |     }
   | |_____^
   = note: expected `BookSide<'a>`
              found `BookSide<'_>`

我可能不应该使用PhantomData?对我来说,这看起来过于复杂和解决方法。我已经发布了full code here

【问题讨论】:

    标签: generics rust iterator lifetime borrow-checker


    【解决方案1】:

    您的问题基本上归结为以下几点。这里有什么问题?

    fn book_iterator<'a>(slice: &[f64]) -> std::slice::Iter<'a, f64> {
        //                      ^^^^^^ needs to be `&'a [f64]`
        slice.iter()
    }
    

    方法book_iterator(&amp;self) 返回一个BookIterator&lt;'a&gt;'a 来自哪里?它当然来自&amp;self,所以注释为&amp;'a self,代码编译:

    use std::marker::PhantomData;
    
    // traits
    
    pub trait BookIterator<'a>: Iterator<Item = f64> {}
    
    pub trait BookSide<'a> {
        type BookIteratorType: BookIterator<'a>;
    
        // 'a added here in trait method signature
        fn book_iterator(&'a self) -> Self::BookIteratorType;
    }
    
    // implementation 1: stateful
    
    pub struct ArrayBookSide {
        quotes: Vec<f64>,
    }
    
    pub struct ArrayBookSideIterator<'a> {
        quotes_iter: std::slice::Iter<'a, f64>,
    }
    
    impl<'a> BookSide<'a> for ArrayBookSide {
        type BookIteratorType = ArrayBookSideIterator<'a>;
    
        // 'a added here below
        fn book_iterator(&'a self) -> Self::BookIteratorType {
            ArrayBookSideIterator {
                quotes_iter: self.quotes.iter(),
            }
        }
    }
    
    impl<'a> Iterator for ArrayBookSideIterator<'a> {
        type Item = f64;
    
        fn next(&mut self) -> Option<Self::Item> {
            self.quotes_iter.next().map(|&quote| quote)
        }
    }
    
    impl<'a> BookIterator<'a> for ArrayBookSideIterator<'a> {}
    
    // implementation 2: delegating
    
    pub struct CommissionBookSide<'a, B>
    where
        B: BookSide<'a>,
    {
        base_book_side: B,
        multiplier: f64,
        _marker: PhantomData<&'a B>,
    }
    
    impl<'a, B> CommissionBookSide<'a, B>
    where
        B: BookSide<'a>,
    {
        pub fn new(base_book_side: B) -> CommissionBookSide<'a, B> {
            CommissionBookSide {
                base_book_side,
                multiplier: 1.1,
                _marker: PhantomData {},
            }
        }
    }
    
    impl<'a, B> BookSide<'a> for CommissionBookSide<'a, B>
    where
        B: BookSide<'a>,
    {
        type BookIteratorType = CommissionIterator<'a, B::BookIteratorType>;
    
        // 'a added here before
        fn book_iterator(&'a self) -> Self::BookIteratorType {
            CommissionIterator {
                base_iterator: self.base_book_side.book_iterator(),
                multiplier: self.multiplier,
                _marker: PhantomData {},
            }
        }
    }
    
    pub struct CommissionIterator<'a, BI>
    where
        BI: BookIterator<'a>,
    {
        base_iterator: BI,
        multiplier: f64,
        _marker: PhantomData<&'a BI>,
    }
    
    impl<'a, BI> Iterator for CommissionIterator<'a, BI>
    where
        BI: BookIterator<'a>,
    {
        type Item = BI::Item;
    
        fn next(&mut self) -> Option<Self::Item> {
            self.base_iterator
                .next()
                .map(|quote| quote * self.multiplier)
        }
    }
    
    impl<'a, BI> BookIterator<'a> for CommissionIterator<'a, BI> where BI: BookIterator<'a> {}
    

    playground

    【讨论】:

    • 非常感谢!现在它可以工作了,它看起来很简单!
    • 我还有一个问题。为什么要注解self?它已经在声明中使用相同的生命周期名称进行了注释,不是吗?看起来像重复。
    • 声明 BookIterator&lt;'a&gt;BookIterator 在生命周期中是通用的,但仍然没有迹象表明这些生命周期可能来自哪里,它们可能来自 &amp;self 或者它们可能完全来自其他地方,这就是为什么您仍然需要将&amp;self 显式注释为&amp;'a self,因为在这种特殊情况下它们来自&amp;self
    猜你喜欢
    • 2010-10-31
    • 2016-04-08
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2015-06-28
    • 1970-01-01
    相关资源
    最近更新 更多