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
142 views
in Technique[技术] by (71.8m points)

generics - How to implement decorator in Rust?

I am learning Rust and I am stuck with a toy example. I have already read documentation on lifetimes, this post and a bunch of questions on Stack Overflow. I've spent more than a week, but I'm still stuck, so I decided to ask for help from a community.

I have a generic trait BookSide which returns BookIterator (which extends usual Iterator). And I have two implementations for both BookSide and BookIterator: ArrayBookSide and CommissionBookSide.

  1. First one is stateful. It has a Vec under the hood.
  2. Second one is stateless: it wraps some other BookSide.

My goal is simply to compile the whole thing. I was solving problems and followed the suggestions of the compiler. This process resulted in the following code.

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> {}

Now I have the following compile error.

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<'_>`

I probably shouldn't use PhantomData? It looks like an over complication and a workaround to me. I have published the full code here.

question from:https://stackoverflow.com/questions/65864390/how-to-implement-decorator-in-rust

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

1 Reply

0 votes
by (71.8m points)

Your issue essentially boils down to the following. What's the issue here?

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

The method book_iterator(&self) returns a BookIterator<'a>. Where does the 'a come from? It comes from &self of course, so annotate it as &'a self and the code compiles:

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


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

...