How do I specify "where" annotations in a trait in Rust? - rust

I'm trying to re-implement Vec::retain() with print statements so that I can figure out how it works, but I'm stuck on this extended type annotation where F: FnMut(&T) -> bool. I understand why it's there, but I can't figure out how to annotate it in the trait declaration so it stops throwing errors (and lets me fix the other ones in the code):
trait TestVec {
fn retain_with_prints<F>(&mut self, mut f: F);
}
impl<T> TestVec for Vec<T> {
fn retain_with_prints<F>(&mut self, mut f: F)
where F: FnMut(&T) -> bool
{
let len = self.len();
let mut del = 0;
{
let v = &mut **self;
for i in 0..len {
println!("on position: {}", &i);
if !f(&v[i]) {
del += 1;
println!("incremented del to: {}", del);
} else if del > 0 {
println!("swapping {} for {}", v[i - del], v[i]);
v.swap(i - del, i);
}
}
}
if del > 0 {
println!("removing last {} elements of vector", del);
self.truncate(len - del);
}
}
}
fn main() {
let v = vec![0,1,2,3,4,5];
v.retain_with_prints(|item| { item % 2 == 0 });
}
Errors:
As is: error: the requirement `for<'r> F: std::ops::FnMut<(&'r T,)>` appears on the impl method but not on the corresponding trait method [E0276]
Adding where clause to trait: error: type name `T` is undefined or not in scope [E0412]
The compiler doesn't seem to like it if I try to specify trait<T> either, and I can't seem to get the right thing to come up in search results.
How do I specify this?

You need to parameterize the trait:
trait TestVec<T> {
fn retain_with_prints<F>(&mut self, mut f: F)
where F: FnMut(&T) -> bool;
}
And also link the types at implementation time.
impl<T> TestVec<T> for Vec<T>
Beyond that, you will need to require that your T implements Display and make your variable mutable:
impl<T> TestVec<T> for Vec<T>
where T: std::fmt::Display
{
let mut v = vec![0,1,2,3,4,5];

Related

How to return a reference to a field in an Iterator? [duplicate]

I am having trouble expressing the lifetime of the return value of an Iterator implementation. How can I compile this code without changing the return value of the iterator? I'd like it to return a vector of references.
It is obvious that I am not using the lifetime parameter correctly but after trying various ways I just gave up, I have no idea what to do with it.
use std::iter::Iterator;
struct PermutationIterator<T> {
vs: Vec<Vec<T>>,
is: Vec<usize>,
}
impl<T> PermutationIterator<T> {
fn new() -> PermutationIterator<T> {
PermutationIterator {
vs: vec![],
is: vec![],
}
}
fn add(&mut self, v: Vec<T>) {
self.vs.push(v);
self.is.push(0);
}
}
impl<T> Iterator for PermutationIterator<T> {
type Item = Vec<&'a T>;
fn next(&mut self) -> Option<Vec<&T>> {
'outer: loop {
for i in 0..self.vs.len() {
if self.is[i] >= self.vs[i].len() {
if i == 0 {
return None; // we are done
}
self.is[i] = 0;
self.is[i - 1] += 1;
continue 'outer;
}
}
let mut result = vec![];
for i in 0..self.vs.len() {
let index = self.is[i];
result.push(self.vs[i].get(index).unwrap());
}
*self.is.last_mut().unwrap() += 1;
return Some(result);
}
}
}
fn main() {
let v1: Vec<_> = (1..3).collect();
let v2: Vec<_> = (3..5).collect();
let v3: Vec<_> = (1..6).collect();
let mut i = PermutationIterator::new();
i.add(v1);
i.add(v2);
i.add(v3);
loop {
match i.next() {
Some(v) => {
println!("{:?}", v);
}
None => {
break;
}
}
}
}
(Playground link)
error[E0261]: use of undeclared lifetime name `'a`
--> src/main.rs:23:22
|
23 | type Item = Vec<&'a T>;
| ^^ undeclared lifetime
As far as I understand, you want want the iterator to return a vector of references into itself, right? Unfortunately, it is not possible in Rust.
This is the trimmed down Iterator trait:
trait Iterator {
type Item;
fn next(&mut self) -> Option<Item>;
}
Note that there is no lifetime connection between &mut self and Option<Item>. This means that next() method can't return references into the iterator itself. You just can't express a lifetime of the returned references. This is basically the reason that you couldn't find a way to specify the correct lifetime - it would've looked like this:
fn next<'a>(&'a mut self) -> Option<Vec<&'a T>>
except that this is not a valid next() method for Iterator trait.
Such iterators (the ones which can return references into themselves) are called streaming iterators. You can find more here, here and here, if you want.
Update. However, you can return a reference to some other structure from your iterator - that's how most of collection iterators work. It could look like this:
pub struct PermutationIterator<'a, T> {
vs: &'a [Vec<T>],
is: Vec<usize>
}
impl<'a, T> Iterator for PermutationIterator<'a, T> {
type Item = Vec<&'a T>;
fn next(&mut self) -> Option<Vec<&'a T>> {
...
}
}
Note how lifetime 'a is now declared on impl block. It is OK to do so (required, in fact) because you need to specify the lifetime parameter on the structure. Then you can use the same 'a both in Item and in next() return type. Again, that's how most of collection iterators work.
#VladimirMatveev's answer is correct in how it explains why your code cannot compile. In a nutshell, it says that an Iterator cannot yield borrowed values from within itself.
However, it can yield borrowed values from something else. This is what is achieved with Vec and Iter: the Vec owns the values, and the the Iter is just a wrapper able to yield references within the Vec.
Here is a design which achieves what you want. The iterator is, like with Vec and Iter, just a wrapper over other containers who actually own the values.
use std::iter::Iterator;
struct PermutationIterator<'a, T: 'a> {
vs : Vec<&'a [T]>,
is : Vec<usize>
}
impl<'a, T> PermutationIterator<'a, T> {
fn new() -> PermutationIterator<'a, T> { ... }
fn add(&mut self, v : &'a [T]) { ... }
}
impl<'a, T> Iterator for PermutationIterator<'a, T> {
type Item = Vec<&'a T>;
fn next(&mut self) -> Option<Vec<&'a T>> { ... }
}
fn main() {
let v1 : Vec<i32> = (1..3).collect();
let v2 : Vec<i32> = (3..5).collect();
let v3 : Vec<i32> = (1..6).collect();
let mut i = PermutationIterator::new();
i.add(&v1);
i.add(&v2);
i.add(&v3);
loop {
match i.next() {
Some(v) => { println!("{:?}", v); }
None => {break;}
}
}
}
(Playground)
Unrelated to your initial problem. If this were just me, I would ensure that all borrowed vectors are taken at once. The idea is to remove the repeated calls to add and to pass directly all borrowed vectors at construction:
use std::iter::{Iterator, repeat};
struct PermutationIterator<'a, T: 'a> {
...
}
impl<'a, T> PermutationIterator<'a, T> {
fn new(vs: Vec<&'a [T]>) -> PermutationIterator<'a, T> {
let n = vs.len();
PermutationIterator {
vs: vs,
is: repeat(0).take(n).collect(),
}
}
}
impl<'a, T> Iterator for PermutationIterator<'a, T> {
...
}
fn main() {
let v1 : Vec<i32> = (1..3).collect();
let v2 : Vec<i32> = (3..5).collect();
let v3 : Vec<i32> = (1..6).collect();
let vall: Vec<&[i32]> = vec![&v1, &v2, &v3];
let mut i = PermutationIterator::new(vall);
}
(Playground)
(EDIT: Changed the iterator design to take a Vec<&'a [T]> rather than a Vec<Vec<&'a T>>. It's easier to take a ref to container than to build a container of refs.)
As mentioned in other answers, this is called a streaming iterator and it requires different guarantees from Rust's Iterator. One crate that provides such functionality is aptly called streaming-iterator and it provides the StreamingIterator trait.
Here is one example of implementing the trait:
extern crate streaming_iterator;
use streaming_iterator::StreamingIterator;
struct Demonstration {
scores: Vec<i32>,
position: usize,
}
// Since `StreamingIterator` requires that we be able to call
// `advance` before `get`, we have to start "before" the first
// element. We assume that there will never be the maximum number of
// entries in the `Vec`, so we use `usize::MAX` as our sentinel value.
impl Demonstration {
fn new() -> Self {
Demonstration {
scores: vec![1, 2, 3],
position: std::usize::MAX,
}
}
fn reset(&mut self) {
self.position = std::usize::MAX;
}
}
impl StreamingIterator for Demonstration {
type Item = i32;
fn advance(&mut self) {
self.position = self.position.wrapping_add(1);
}
fn get(&self) -> Option<&Self::Item> {
self.scores.get(self.position)
}
}
fn main() {
let mut example = Demonstration::new();
loop {
example.advance();
match example.get() {
Some(v) => {
println!("v: {}", v);
}
None => break,
}
}
example.reset();
loop {
example.advance();
match example.get() {
Some(v) => {
println!("v: {}", v);
}
None => break,
}
}
}
Unfortunately, streaming iterators will be limited until generic associated types (GATs) from RFC 1598 are implemented.
I wrote this code not long ago and somehow stumbled on this question here. It does exactly what the question asks: it shows how to implement an iterator that passes its callbacks a reference to itself.
It adds an .iter_map() method to IntoIterator instances. Initially I thought it should be implemented for Iterator itself, but that was a less flexible design decision.
I created a small crate for it and posted my code to GitHub in case you want to experiment with it, you can find it here.
WRT the OP's trouble with defining lifetimes for the items, I didn't run into any such trouble implementing this while relying on the default elided lifetimes.
Here's an example of usage. Note the parameter the callback receives is the iterator itself, the callback is expected to pull the data from it and either pass it along as is or do whatever other operations.
use iter_map::IntoIterMap;
let mut b = true;
let s = "hello world!".chars().peekable().iter_map(|iter| {
if let Some(&ch) = iter.peek() {
if ch == 'o' && b {
b = false;
Some('0')
} else {
b = true;
iter.next()
}
} else { None }
}).collect::<String>();
assert_eq!(&s, "hell0o w0orld!");
Because the IntoIterMap generic trait is implemented for IntoIterator, you can get an "iter map" off anything that supports that interface. For instance, one can be created directly off an array, like so:
use iter_map::*;
fn main()
{
let mut i = 0;
let v = [1, 2, 3, 4, 5, 6].iter_map(move |iter| {
i += 1;
if i % 3 == 0 {
Some(0)
} else {
iter.next().copied()
}
}).collect::<Vec<_>>();
assert_eq!(v, vec![1, 2, 0, 3, 4, 0, 5, 6, 0]);
}
Here's the full code - it was amazing it took such little code to implement, and everything just seemed to work smoothly while putting it together. It gave me a new appreciation for the flexibility of Rust itself and its design decisions.
/// Adds `.iter_map()` method to all IntoIterator classes.
///
impl<F, I, J, R, T> IntoIterMap<F, I, R, T> for J
//
where F: FnMut(&mut I) -> Option<R>,
I: Iterator<Item = T>,
J: IntoIterator<Item = T, IntoIter = I>,
{
/// Returns an iterator that invokes the callback in `.next()`, passing it
/// the original iterator as an argument. The callback can return any
/// arbitrary type within an `Option`.
///
fn iter_map(self, callback: F) -> ParamFromFnIter<F, I>
{
ParamFromFnIter::new(self.into_iter(), callback)
}
}
/// A trait to add the `.iter_map()` method to any existing class.
///
pub trait IntoIterMap<F, I, R, T>
//
where F: FnMut(&mut I) -> Option<R>,
I: Iterator<Item = T>,
{
/// Returns a `ParamFromFnIter` iterator which wraps the iterator it's
/// invoked on.
///
/// # Arguments
/// * `callback` - The callback that gets invoked by `.next()`.
/// This callback is passed the original iterator as its
/// parameter.
///
fn iter_map(self, callback: F) -> ParamFromFnIter<F, I>;
}
/// Implements an iterator that can be created from a callback.
/// does pretty much the same thing as `std::iter::from_fn()` except the
/// callback signature of this class takes a data argument.
pub struct ParamFromFnIter<F, D>
{
callback: F,
data: D,
}
impl<F, D, R> ParamFromFnIter<F, D>
//
where F: FnMut(&mut D) -> Option<R>,
{
/// Creates a new `ParamFromFnIter` iterator instance.
///
/// This provides a flexible and simple way to create new iterators by
/// defining a callback.
/// # Arguments
/// * `data` - Data that will be passed to the callback on each
/// invocation.
/// * `callback` - The callback that gets invoked when `.next()` is invoked
/// on the returned iterator.
///
pub fn new(data: D, callback: F) -> Self
{
ParamFromFnIter { callback, data }
}
}
/// Implements Iterator for ParamFromFnIter.
///
impl<F, D, R> Iterator for ParamFromFnIter<F, D>
//
where F: FnMut(&mut D) -> Option<R>,
{
type Item = R;
/// Iterator method that returns the next item.
/// Invokes the client code provided iterator, passing it `&mut self.data`.
///
fn next(&mut self) -> Option<Self::Item>
{
(self.callback)(&mut self.data)
}
}

How to send generic T to another thread?

How to send generic T?
I try to send a generic T to another thread but I'm getting:
error[E0308]: mismatched types
--> src/main.rs:23:22
|
23 | t1.merge(Element(vec![3]));
| ^^^^^^^^^^^^^^^^ expected associated type, found struct `Element`
|
= note: expected associated type `<T as Join>::Item`
found struct `Element`
= help: consider constraining the associated type `<T as Join>::Item` to `Element`
Full code:
trait Join {
type Item;
fn merge(&mut self, other: Self::Item);
}
#[derive(Debug, Default)]
struct Element(Vec<u8>);
impl Join for Element {
type Item = Element;
fn merge(&mut self, mut other: Self::Item) {
self.0.append(&mut other.0);
}
}
fn work<T>()
where
T: Default + Join + Send + Sync + 'static,
{
let (sender, receiver) = std::sync::mpsc::channel::<(T)>();
std::thread::spawn(move || {
while let (mut t1) = receiver.recv().unwrap() {
t1.merge(Element(vec![3]));
}
});
loop {
let mut t1 = T::default();
sender.send(t1);
std::thread::sleep(std::time::Duration::from_secs(5));
}
}
fn main() {
// works!
let mut e = Element(vec![1]);
e.merge(Element(vec![2]));
// bad!
work::<Element>();
}
Playground link
When you use generics you let the caller decide which types must be used by your generic function.
This line in your example t1.merge(Element(vec![3])); is invalid because it assumes T = Element but the caller can chose from infinitely many possible types of T where T != Element which is why the compiler is complaining.
To make your function fully generic you have to do something like add a Default bound to <T as Join>::Item in the function signature and then change the offending line to t1.merge(<T as Join>::Item::default());.
Updated working commented example:
use std::fmt::Debug;
trait Join {
type Item;
fn merge(&mut self, other: Self::Item);
}
#[derive(Debug)]
struct Element(Vec<u8>);
// updated Default impl so we can observe merges
impl Default for Element {
fn default() -> Self {
Element(vec![1])
}
}
impl Join for Element {
type Item = Element;
fn merge(&mut self, mut other: Self::Item) {
self.0.append(&mut other.0);
}
}
fn work<T>() -> Result<(), Box<dyn std::error::Error>>
where
T: Default + Join + Send + Sync + Debug + 'static,
<T as Join>::Item: Default, // added Default bound here
{
let (sender, receiver) = std::sync::mpsc::channel::<T>();
std::thread::spawn(move || {
while let Ok(mut t1) = receiver.recv() {
// changed this to use Default impl
t1.merge(<T as Join>::Item::default());
// prints "Element([1, 1])" three times
println!("{:?}", t1);
}
});
let mut iterations = 3;
loop {
let t1 = T::default();
sender.send(t1)?;
std::thread::sleep(std::time::Duration::from_millis(100));
iterations -= 1;
if iterations == 0 {
break;
}
}
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// works!
let mut e = Element(vec![1]);
e.merge(Element(vec![2]));
// now also works!
work::<Element>()?;
Ok(())
}
playground

How to implement a macro that defines a new public type and returns an instance of that type?

I want to implement a struct using macro_rules! because the generics require a lot of boilerplate and trait hunting.
The struct in question has a hash table inside but the key and the value types are to be provided by the user. The code is as follows:
macro_rules! new_ytz {
($T: ty) => {
// define the struct
pub struct Ytz {
table: hashbrown::hash_map::HashMap<$T, $T>,
}
impl Ytz {
pub fn new() -> Self {
Ytz {
table: hashbrown::hash_map::HashMap::<$T, $T>::new(),
}
}
pub fn add(&mut self, item: &$T) {
if self.table.contains_key(item) {
*self.table.get_mut(item).unwrap() += *item;
} else {
self.table.insert(*item, *item);
}
}
pub fn largest(&self) -> $T {
let mut result = 0;
for v in self.table.values() {
if result < *v {
result = *v;
}
}
result
}
}
// construct an instance of the struct and return it
Ytz::new()
};
}
// driver
fn main() {
let mut y = new_ytz!(u64); // should construct the object and return Ytz::new()
y.add(&71);
y.add(&25);
y.add(&25);
y.add(&25);
y.add(&34);
println!("{}", y.largest());
}
This won't compile since it tries to paste the struct within the main function:
error: expected expression, found keyword `pub`
--> src/main.rs:4:9
|
4 | pub struct Ytz {
| ^^^ expected expression
...
40 | let mut y = new_ytz!(u64); // should construct the object and return Ytz::new()
| ------------- in this macro invocation
How can I work around it? How can I paste the struct outside the main function publicly, along with the impl block?
generics require a lot of boilerplate
use std::collections::HashMap;
use core::hash::Hash;
use std::ops::AddAssign;
struct YtzU64<T: Eq + Ord + Hash + Copy + AddAssign> {
table: HashMap<T, T>
}
impl<T: Eq + Ord + Hash + Copy + AddAssign> YtzU64<T> {
pub fn new() -> Self {
Self {
table: HashMap::new()
}
}
pub fn add(&mut self, item: &T) {
if let Some(item) = self.table.get_mut(item) {
*item += *item;
} else {
self.table.insert(*item, *item);
}
}
pub fn largest(&self) -> Option<T> {
let mut values = self.table.values();
let mut largest:Option<T> = values.next().map(|t| *t);
for v in values {
if largest < Some(*v) {
largest = Some(*v);
}
}
largest
}
}
fn main() {
let mut y = YtzU64::new();
y.add(&71);
y.add(&25);
y.add(&25);
y.add(&25);
y.add(&34);
println!("{}", y.largest().unwrap());
}
My translation of your macro requires less boilerplate than your macro. It has two fewer indents, 4 fewer lines (macro_rules!, pattern matching at the top, two close braces at the end). Note that I changed the api slightly, as largest now returns an Option, to match std::iter::Iterator::max(). Also note that your api design is limited to T:Copy. You would have to redesign it a little if you want to support T: ?Copy + Clone or T: ?Copy + ?Clone.
trait hunting
The compiler is your friend. Watch what happens when I remove one of the trait bounds
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
...
Using a macro is an interesting exercise, but re-implementing generics using macros is not useful.

What is the type/trait produced by calling the `chars` method on a string literal?

Consider this piece of code:
struct Collector<T>
where
T: Iterator<Item = char>,
{
current: u32,
right_neighbour: u32,
counter: usize,
iterator: T,
}
impl<T: Iterator<Item = char>> Collector<T> {
fn next(&self) -> u32 {
self.iterator
.next()
.expect("failed to get next digit!")
.to_digit(10)
.expect("failed to prase char as digit!")
}
fn collect(&self) {
self.current = self.right_neighbour;
self.right_neighbour = self.next();
self.counter = self.counter + 1;
}
fn initialize<U>(iterator: U) -> Collector<U>
where
U: Iterator<Item = char>,
{
let mut collector = Collector {
current: 0,
right_neighbour: 0,
counter: 0,
iterator: iterator,
};
collector.collect();
collector
}
}
fn main() {
let numstr = "1111";
let mut collector = Collector::initialize(numstr.chars().cycle().peekable());
}
It produces a type mismatch error:
error[E0284]: type annotations required: cannot resolve `<_ as std::iter::Iterator>::Item == char`
--> src/main.rs:46:25
|
46 | let mut collector = Collector::initialize(numstr.chars().cycle().peekable());
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: required by `<Collector<T>>::initialize`
What is the type of numstr.chars().cycle().peekable()? The compiler tells me that its full type is:
std::iter::Peekable<std::iter::Cycle<std::str::Chars<'_>>>
I know that I can't use that type in the definition of my structs/functions, because it doesn't have an explicit lifetime...
How can I correctly write this code?
What is the type/trait produced by calling the chars method on a string literal?
The documentation for str::chars tells you exactly what type it is:
fn chars(&self) -> Chars
It produces a type mismatch error
Yes, because you haven't specified what the concrete type of T should be. You've introduced a completely separate generic type U and have no arguments or return types that reference T. The compiler has zero context to use to infer what T is.
How can I correctly write this code?
Remove the useless extra type parameter:
struct Collector<T>
where
T: Iterator<Item = char>,
{
current: u32,
right_neighbour: u32,
counter: usize,
iterator: T,
}
impl<T: Iterator<Item = char>> Collector<T> {
fn new(iterator: T) -> Collector<T> {
let mut collector = Collector {
current: 0,
right_neighbour: 0,
counter: 0,
iterator: iterator,
};
collector.collect();
collector
}
fn collect(&self) {
unimplemented!()
}
fn next(&self) -> u32 {
unimplemented!()
}
}
fn main() {
let numstr = "1111";
let mut collector = Collector::new(numstr.chars().cycle().peekable());
}
I've removed the implementations of next and collect because they have other, unrelated errors that I don't care to fix. I also renamed initialize to new, as new is the standard constructor name in the absence of multiple constructors.
Of note is that the usage of peekable here is completely useless. The generic type T doesn't know that it's possible to call peek because there's no appropriate trait bound for such.
because it doesn't have an explicit lifetime
You don't have to care about the lifetime, that will be covered by the generic. If your type needed to know that it was peekable, just put that in your struct:
struct Collector<T>
where
T: Iterator<Item = char>,
{
// ...
iterator: std::iter::Peekable<T>,
}
impl<T: Iterator<Item = char>> Collector<T> {
fn new(iterator: T) -> Collector<T> {
let mut collector = Collector {
// ...
iterator: iterator.peekable(),
};
// ...
}
// ...
}
fn main() {
// ...
let mut collector = Collector::new(numstr.chars().cycle());
}

Specifying lifetimes when using the factory pattern in Rust

The following code doesn't compile:
trait Phone {
fn call(&self);
}
struct IPhone<'a> {
my_str: &'a str
}
impl<'a> Phone for IPhone<'a> {
fn call(&self) {
print!("{}", self.my_str);
}
}
trait Factory<'a, P: Phone> {
fn new_phone(&self, ms: &'a str) -> P;
}
struct IPhoneFactory;
impl<'a> Factory<'a, IPhone<'a>> for IPhoneFactory {
fn new_phone(&self, ms: &'a str) -> IPhone<'a> {
return IPhone {
my_str: ms
};
}
}
fn call_phone<'a, P: Phone, F: Factory<'a, P>>(f: F) {
for _ in 0..10 {
let s = String::new();
let p = f.new_phone(s.as_str());
p.call();
}
}
fn main() {
call_phone(IPhoneFactory);
}
I get the following error:
error: `s` does not live long enough
let p = f.new_phone(s.as_str());
^
note: reference must be valid for the lifetime 'a as defined on the block at 28:53...
fn call_phone<'a, P: Phone, F: Factory<'a, P>>(f: F) {
^
note: ...but borrowed value is only valid for the block suffix following statement 0 at 30:30
let s = String::new();
^
I want to be able to have a factory that returns an abstract class, but when that class takes a reference I can't figure out how to specify the lifetime properly.
You're right about that:
There is no reason for the reference to live as long as the factory, it only needs to live as long as the object the factory is creating (the factory itself doesn't store a reference to the string).
But the bound on call_phone says something different
fn call_phone<'a, P: Phone, F: Factory<'a, P>>(f: F) { ... }
That code says that there's a single lifetime for the whole factory, which will be used for each phone. You want something different, you want to say that f is a good factory for any lifetime:
fn call_phone<..., F: for<'a> Factory<'a, ...>>(f: F) { ... }
The other problem is that in the Factory trait definition:
trait Factory<'a, P: Phone> {
fn new_phone(&self, ms: &'a str) -> P;
}
There's nothing tying lifetime of P to ms. The trait definition allows the returned phone to outlive the string, which should definitely be forbidden for the IPhone implementation! So, to fix it, we add a lifetime parameter to the Phone trait:
trait Phone<'a> {
fn call(&self);
}
But there's still one problem. We can't really write that signature:
fn call_phone<P: ???, F: for<'a> Factory<'a, P<'a>>(f: F) { ... }
Since we want P to be not a type, but rather a family of types (more precisely, a lifetime → type constructor). Remember, the phone in each iteration of loop has a different type (since the lifetime is a part of a type, and lifetimes in different iterations of loops are different).
Ability to express such a signature is planned for the future Rust, but for now, we have to make a workaround and make the phone associated type of Factory trait:
trait Phone<'a> {
fn call(&self);
}
struct IPhone<'a> {
my_str: &'a str
}
impl<'a> Phone<'a> for IPhone<'a> {
fn call(&self) {
println!("{}", self.my_str);
}
}
trait Factory<'a> {
type Output: Phone<'a>;
fn new_phone(&self, ms: &'a str) -> Self::Output;
}
struct IPhoneFactory;
impl<'a> Factory<'a> for IPhoneFactory {
type Output = IPhone<'a>;
fn new_phone(&self, ms: &'a str) -> IPhone<'a> {
IPhone {
my_str: ms
}
}
}
fn call_phone<F: for<'a> Factory<'a>>(f: F) {
for i in 0..10 {
let s = i.to_string();
let p = f.new_phone(&s);
p.call();
}
}
fn main() {
call_phone(IPhoneFactory);
}
Associated type allows the factory to produce only one kind of product, which is maybe what you wanted. If you want different implementations of Factory to have different Outputs, you can achieve this by using phantom types:
trait Phone<'a> {
type Phantom;
fn call(&self);
}
enum IPhonePhantom {}
struct IPhone<'a> {
my_str: &'a str
}
impl<'a> Phone<'a> for IPhone<'a> {
type Phantom = IPhonePhantom;
fn call(&self) {
println!("{}", self.my_str);
}
}
trait Factory<'a, Selector> {
type Output: Phone<'a, Phantom=Selector>;
fn new_phone(&self, ms: &'a str) -> Self::Output;
}
struct MyFactory;
impl<'a> Factory<'a, IPhonePhantom> for MyFactory {
type Output = IPhone<'a>;
fn new_phone(&self, ms: &'a str) -> IPhone<'a> {
IPhone {
my_str: ms
}
}
}
fn call_phone<Selector, F: for<'a> Factory<'a, Selector>>(f: F) {
for i in 0..10 {
let s = i.to_string();
let p = f.new_phone(&s);
p.call();
}
}
fn main() {
call_phone::<IPhonePhantom, _>(MyFactory);
}
The Phantom associated type on the Phone trait is not strictly necessary, it's only needed to tie the phone type to its phantom type and to make sure Factory implementors don't lie.
Your problem is here:
fn call_phone<'a, P: Phone, F: Factory<'a, P>>(f: F) {
// Factory has a lifetime 'a ----------^
// that is at least as long as the scope of call_phone
for _ in 0..10 {
let s = String::new(); // s is born here
let p = f.new_phone(s.as_str());
// new reference ---^
// new_phone definition requires this to have
// the same lifetime 'a as the Factory
p.call();
}
// s is destroyed here, no references to s can
// still exist
} // F is still alive
One thing you can do is passing the &str as a parameter to call_phone, to make sure the reference lives as long as the function:
fn call_phone<'a, P: Phone, F: Factory<'a, P>>(f: F, s: &'a str) {
for _ in 0..10 {
let p = f.new_phone(s);
p.call();
}
}
fn main() {
call_phone(IPhoneFactory, &"hello");
}
Another one is not working with references, but let your struct IPhone own the String

Resources