impl Trait for primitives and references of [duplicate] - rust

This question already has answers here:
What are Rust's exact auto-dereferencing rules?
(4 answers)
Closed 9 months ago.
Perhaps it is a simple question, but I can't understand why just impl'ing Trait for the primitive, owned type, I got for free the same impl for reference types...
trait Cool: Sized + std::fmt::Debug {
fn cool(self) {
println!("cool -> {:?}", self);
}
}
impl Cool for i32 {}
// it still works if this line is uncommented...
// impl Cool for &i32 {}
fn main(){
let val = 123;
val.cool();
(&val).cool();
}
Playground

It is not just because of primitives, it will work for all types that implement Copy. Will not work otherwise:
trait Cool: Sized + std::fmt::Debug {
fn cool(self) {
println!("cool -> {:?}", self);
}
}
#[derive(Debug)]
struct NonCopy;
impl Cool for i32 {}
impl Cool for NonCopy {}
fn main(){
let val = 123;
val.cool();
(&val).cool();
let nc = NonCopy{};
nc.cool();
(&nc).cool();
}
Fails with a clear error code:
error[E0507]: cannot move out of a shared reference
--> src/main.rs:20:5
|
20 | (&nc).cool();
| ^^^^^^------
| | |
| | value moved due to this method call
| move occurs because value has type `NonCopy`, which does not implement the `Copy` trait
|
Playground
What it's happening is that with the Copy types rust creates a copy transparently for you when needed.
Note that it fails even if we comment out the previous line // nc.cool();, which obviously moves the value...

That's auto-dereferencing; it applies whenever you use the . operator. It's meant to erase the distinction between . and -> which exists in C and related languages.
It was introduced in RFC 241.

Related

Cloning an Rc pointer over a trait object in Rust?

I am learning Rust and don't understand why the following doesnt work. I gather we are unable to clone an Rc pointer over a trait object? How am I to pass such a reference to an function defined only by a trait, as attempted in some_function?
use std::rc::Rc;
trait SomeTrait {}
struct SomeThing {}
impl SomeTrait for SomeThing {}
fn some_function(s: Rc<dyn SomeTrait>) {}
fn another_function(s: &Rc<dyn SomeTrait>) {}
fn main() {
let s = Rc::new(SomeThing{});
// This doesnt work
some_function(Rc::clone(&s));
// I could do this
some_function(s);
// But then I could not do this
some_function(s);
// For that matter, neither can I do this
another_function(&s);
}
If you look at the error messages of the compiler you will see this:
error[E0308]: mismatched types
|
14 | some_function(Rc::clone(&s));
| ^^ expected trait object `dyn SomeTrait`, found struct `SomeThing`
|
= note: expected reference `&Rc<dyn SomeTrait>`
found reference `&Rc<SomeThing>`
That means the compiler mis-infers the type of s to Rc<SomeThing> instead of the Rc<dyn SomeTrait> you were looking for, which can be confirmed by the usual trick of providing a blatantly incorrect type to let s:
error[E0308]: mismatched types
--> src/main.rs:11:17
|
11 | let s: () = Rc::new(SomeThing{});
| -- ^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `Rc`
| |
| expected due to this
|
= note: expected unit type `()`
found struct `Rc<SomeThing>`
Since Rust wrappers are invariant, an Rc<SomeThing> and an Rc<dyn SomeTrait> are completely incompatible values, there is no way to just use one for the other.
The solution is to simply explicitely type s correctly:
use std::rc::Rc;
trait SomeTrait {}
struct SomeThing {}
impl SomeTrait for SomeThing {}
fn some_function(s: Rc<dyn SomeTrait>) {}
fn another_function(s: &Rc<dyn SomeTrait>) {}
fn main() {
let s: Rc<dyn SomeTrait> = Rc::new(SomeThing{});
// This doesnt work
some_function(Rc::clone(&s));
// For that matter, neither can I do this
another_function(&s);
}
Obviously the two calls in the middle can't work, as the first one will move the local Rc, which the second one (and the final call) still want.

Lifetimes when returning a reference to the contents of Rc<RefCell<_>> [duplicate]

This question already has answers here:
How do I return a reference to something inside a RefCell without breaking encapsulation?
(3 answers)
How do I borrow a RefCell<HashMap>, find a key, and return a reference to the result? [duplicate]
(1 answer)
Closed 2 years ago.
How can I return a reference to something from inside a shared pointer (in this case Rc<RefCell<_>>)? In the example below, I show how it can be done with just a regular mutable reference to self, but if it becomes a shared pointer instead, the compiler gets angry that the return type has a missing lifetime specifier.
error[E0106]: missing lifetime specifier
--> src/main.rs:19:60
|
19 | fn add_to_shared(me: Rc<RefCell<Thing>>, item: i32) -> &i32 {
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
help: consider using the `'static` lifetime
|
19 | fn add_to_shared(me: Rc<RefCell<Thing>>, item: i32) -> &'static i32 {
| ^^^^^^^^
use std::cell::RefCell;
use std::rc::Rc;
struct Thing {
items: Vec<i32>,
}
impl Thing {
fn new() -> Self {
Thing { items: Vec::new() }
}
fn add_to_self(&mut self, item: i32) -> &i32 {
self.items.push(item);
self.items.last().unwrap()
}
// does not compile
fn add_to_shared(me: Rc<RefCell<Thing>>, item: i32) -> &i32 {
me.borrow().items.push(item);
me.borrow().items.last().unwrap()
}
}
fn main() {
let mut thing = Thing::new();
println!("{}", thing.add_to_self(10));
let mut rc = Rc::new(RefCell::new(Thing::new()));
println!("{}", rc.add_to_shared(20));
}
Why do I want to do this? I have a program that builds a tree-like structure with multiple ownership. One of the associated methods takes two nodes of the tree (each shared pointers) and bundles them together into another part of the tree. Each method returns a reference to the newly created node so that it can be conveniently logged out (see the example).
I was thinking I'd need to use lifetime annotations to get this to work, but I have not been able to find out how do apply this concept to the interior of a Rc<RefCell<_>> type.
I think the problem here is that Rust knows how long self lives, but is not able to figure out how long Rc<RefCell<_>> exists. Do you need to return i32 references? If you would return just i32, the value would be copied and you would not have a reference into a struct that might not exists long enough.

Create wrapper over a mutable reference with `Copy`-like semantics [duplicate]

This question already has answers here:
Is it possible to create a wrapper around an &mut that acts like an &mut
(1 answer)
Why is the mutable reference not moved here?
(4 answers)
Closed 2 years ago.
I've been adapting some old code I wrote, one of them had the following (simplified):
pub fn a(x: &mut i32) {
for i in 0..10 {
b(x);
}
}
pub fn b(_x: &mut i32) {
}
which worked fine, even though &mut i32 isn't Copy.
I wanted to restrict what methods could be called on the underlying type, (as instead of &mut i32 I had something along the lines of &mut Vec<...>), so I created a wrapper type over the mutable reference:
#[derive(Debug)]
pub struct I32RefMut<'a>(&'a mut i32);
And I attempted to rewrite a and b using this wrapper as follows:
pub fn a2(x: I32RefMut) {
for _i in 0..10 {
b2(x);
}
}
pub fn b2(_x: I32RefMut) {
}
This gives the following error
17 | pub fn a2(x: I32RefMut) {
| - move occurs because `x` has type `I32RefMut<'_>`, which does not implement the `Copy` trait
18 | for _i in 0..10 {
19 | b2(x);
| ^ value moved here, in previous iteration of loop
Playground link
Which is understandable, as x gets moved into b2 on the first iteration of the loop.
Unfortunately I cannot implement Clone nor Copy, as there may only be 1 mutable reference to the object at a time.
My question is how does &mut i32 work around this and how can I implement this workaround (or similar) on my type I32RefMut.
If possible I'd like to avoid unsafe code as much as possible, such as using #[repr(transparent)] struct I32Wrapper(i32) and then transmuting &mut i32 to &mut I32Wrapper, unless a safe wrapper of this type of operation exists already.
EDIT:
Found a "hack" solution, but I'm not very happy about how it looks, so I'll leave the question open. If no other solutions are found, I'll post it as an answer.
If the call to b2 is changed to b2( I32RefMut(x.0) ), then it successfully compiles. This however, cannot be generalised to a function as such:
impl<'a> I32RefMut<'a> {
pub fn my_clone<'b: 'a>(&'b mut self) -> I32RefMut<'b> {
I32RefMut( self.0 )
}
}
As when we try to call it the compiler tells us we can't borrow x mutably twice.
As this wrapper type is supposed to be defined in a library, I cannot expose it's internal reference, as the whole point of the wrapper was to restrain what the user can call on the reference.
The thing is that you are taking ownership of your new type instance when calling b. Just take a reference to your type instead to get access to the underlaying type:
pub fn a2(x: &I32RefMut) {
for _i in 0..10 {
b2(x);
}
}
pub fn b2(_x: &I32RefMut) {
}
Playground
Actually if you want to mutate it you need to play around a bit with them:
pub fn a2(mut x: I32RefMut) {
for _i in 0..10 {
b2(&mut x);
}
}
pub fn b2(_x: &mut I32RefMut) {
*(_x.0) += 1
}
Playground

Trait mismatch for function argument

I've got one piece of Rust code that compiles and one that's very similar that does not.
The one that works:
pub fn do_something(_: Box<Iterator<Item = f64>>) {}
fn main() {
let iter = Box::new(vec![1.0].into_iter());
do_something(iter);
}
The one that fails:
pub fn do_something(_: Box<Box<Iterator<Item = f64>>>) {}
fn main() {
let iter = Box::new(Box::new(vec![1.0].into_iter()));
do_something(iter);
}
The difference is I have a Box<Box<..>> instead of a Box<..>
I get the following error:
error[E0308]: mismatched types
--> src/main.rs:5:18
|
5 | do_something(iter);
| ^^^^ expected trait std::iter::Iterator, found struct `std::vec::IntoIter`
|
= note: expected type `std::boxed::Box<std::boxed::Box<std::iter::Iterator<Item=f64> + 'static>>`
found type `std::boxed::Box<std::boxed::Box<std::vec::IntoIter<{float}>>>`
I'm interpreting this error to say "IntoIter does not have the trait Iterator" .. but it does. What's the issue?
You can't coerce a Box<Box<I>> into a Box<Box<Iterator<Item = f64>>>, for reasons discussed in this question, but you can coerce the inner Box:
pub fn do_something(_: Box<Box<Iterator<Item = f64>>>) {}
fn main() {
let iter = Box::new(Box::new(vec![1.0].into_iter()) as Box<Iterator<Item = f64>>);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
do_something(iter);
}
Playground.
This works because a cast is a coercion site. By writing as Box<Iterator<Item = f64>>, you're hinting to the compiler that it should attempt to make the expression to the left fit that type instead of inferring Box<IntoIter<f64>>, because once it's wrapped up in the "outer" Box, you can't change it anymore.
Alternatively (but less clearly), you could make Box::new(...) a coercion site by explicitly parameterizing Box:
let iter = Box::<Box<Iterator<Item = f64>>>::new(Box::new(vec![1.0].into_iter()));
Which effectively does the same thing.
To be honest, I'm no expert in Rust at all, but my expectation would have been that both of the snippets you show do not compile. That is because, as you pointed out, Iterator is a trait and not a type and basically you want do_something to receive any type which implements Iterator. Maybe there exists a shortcut such that the compiler can transform the signature into a generic if one of the types is a trait which could be why is sometimes works, but then I'm also not familiar with the Rust language specification enough.
Instead of having do_something take something of type Iterator (?) make it a generic of type T where T is trait bound.
pub fn do_something<T>(_: Box<Box<T>>)
where T: Iterator<Item = f64> + Send {}
fn main() {
let iter = Box::new(Box::new(vec![1.0].into_iter()));
do_something(iter);
}
Playground
Alternatively, you constrain do_something entirely to std::vec::IntoIter and only take parameters of that type.
pub fn do_something(_: Box<Box<std::vec::IntoIter<f64>>>) {}
fn main() {
let iter = Box::new(Box::new(vec![1.0].into_iter()));
do_something(iter);
}
Playground

Convert boxed trait to mutable trait reference in Rust

I am having some trouble with the dynamic dispatch pointer types in Rust. I want to convert a value of type Box<MyTrait> to &mut MyTrait to pass to a function. For example, I tried:
use std::borrow::BorrowMut;
trait MyTrait {
fn say_hi(&mut self);
}
struct MyStruct { }
impl MyTrait for MyStruct {
fn say_hi(&mut self) {
println!("hi");
}
}
fn invoke_from_ref(value: &mut MyTrait) {
value.say_hi();
}
fn main() {
let mut boxed_trait: Box<MyTrait> = Box::new(MyStruct {});
invoke_from_ref(boxed_trait.borrow_mut());
}
This fails with the following error:
error: `boxed_trait` does not live long enough
--> <anon>:22:5
|
21 | invoke_from_ref(boxed_trait.borrow_mut());
| ----------- borrow occurs here
22 | }
| ^ `boxed_trait` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
Strangely enough, this works for &MyTrait but not for &mut MyTrait. Is there any way I can get this conversion to work in the mutable case?
I think you're running into a limitation of the current compiler's lifetime handling. borrow_mut, being a function, imposes stricter lifetime requirements than necessary.
Instead, you can take a mutable borrow to the box's interior by first dereferencing the box, like this:
fn main() {
let mut boxed_trait: Box<MyTrait> = Box::new(MyStruct {});
invoke_from_ref(&mut *boxed_trait);
}

Resources