Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
68: Consume Channel and return the owned Vector of Items r=frewsxcv a=alatiera This allows to consume the `Channel` and get the owned vector/slice of `Items`. This can avoid the reallocation/clone of the whole Items slice in situations where you need to pass the ownership of the data, like when using futures. For example the compiler rightfully complains when trying to do the following: ```rust extern crate rss; extern crate futures; use futures::prelude::*; use futures::stream; use rss::{Item, Channel}; use std::error::Error; struct Foo; fn foo(_item: &Item) -> Box<Future<Item = Foo, Error = Box<Error>>> { unimplemented!() } fn bar(channel: Channel) -> Box<Future<Item = Vec<Foo>, Error = Box<Error>>> { let stream = stream::iter_ok::<_, Box<Error>>(channel.items()); let results = stream.and_then(|item| foo(item)).collect(); Box::new(results) } fn main() {} ``` Since you can only get a reference to an `Item` the compiler can't guarantee that it will satisfy the `'static` lifetime requirement. ```sh error[E0597]: `channel` does not live long enough --> src/main.rs:17:51 | 17 | let stream = stream::iter_ok::<_, Box<Error>>(channel.items()); | ^^^^^^^ borrowed value does not live long enough ... 20 | } | - borrowed value only lives until here | = note: borrowed value must be valid for the static lifetime... error: aborting due to previous error error: Could not compile `foo`. ``` A way to get the above example to compile is make the signature of `fn bar` the following: ```rust fn bar<'a>(channel: &'a Channel) -> Box<Future<Item = Vec<Foo>, Error = Box<Error>> + 'a > { ``` But that only moves the ownership problem in a higher part of the code, since you can never guarantee that `channel` will live until the `Future` is resolved. Co-authored-by: Jordan Petridis <jordanpetridis@protonmail.com> Co-authored-by: Corey Farwell <coreyf@rwell.org>
- Loading branch information