mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-04 17:22:19 +00:00
delegation: add support for wrapping of the return value with `From::from`
This PR supports wrapping the return value of the delegation with `From::from(...)` call. This wrapping is applied only if we understand that `Self` generic param is present in the output of the signature function. The default case are output types such as `Rc<Self>`, `Box<Self>`, etc. So given
```rust
trait MyAdd {
fn add(self, other: Self) -> Box<Self>;
}
impl MyAdd for usize {
fn add(self, other: usize) -> Box<usize> {
Box::new(self + other)
}
}
#[derive(Eq, PartialEq, Debug)]
struct W(Box<usize>);
reuse impl MyAdd for W { *self.0 }
// Desugaring:
#[attr = Inline(Hint)]
fn add(self: _, arg1: _) ->_ {
From::from(Self { 0: MyAdd::add(*self.0, *self.0) })
}
```
As return type of the trait is `Box<Self>` for the delegation to work we need to wrap the return value in the `From::from` call.
## Many from calls
Now only single from is generated which limits the number of supported cases. Consider the return type `Box<Box<Self>>` , in order to support such chains of types we need to generate several `From::from` calls: `From::<Box<_>>::from(From::from(...))`. In such cases there are two problems:
- First we need to create a heuristic two understand how many `From::from` calls should be generated, it is easy for pointer types like `Box`, `Arc`, etc., however if custom types with complex type trees will be considered, the choice is not that obvious: `Struct<Box<Rc<Self>>, Arc<Self>>`. This approach is useful for pointer types but it does not cover all cases in general,
- Next, there will be problems with `From` generic arg inference, if we comeback to case with `Box<Box<Self>>` and generate the following `from` chain: `From::from(From::from(...))` we would get an inference error:
```rust
fn f() -> Box<Box<Box<usize>>> {
Box::from(Box::from(Box::from(1)))
}
// Error:
error[E0283]: type annotations needed
--> src/main.rs:5:9
|
5 | Box::from(Box::from(Box::from(1)))
| ^^^ cannot infer type for struct `Box<_, _>`
|
= note: multiple `impl`s satisfying `Box<Box<Box<usize>>>: From<Box<_, _>>` found in the following crates: `alloc`, `core`:
- impl<T> From<T> for Box<T>;
- impl<T> From<T> for T;
```
We need to explicitly specify generics of `From` trait for this to work, and if we have non-trivial type trees it is not obvious whose generics to specify, in case of chains of pointers it is easy, but in general it is not trivial.
Given those concerns for now we generate a single `From::from` call, which supports simple cases like `Box<Self>`, and if a complex type is used then the user should implement a `From` trait for it.
## Alternative designs
As an alternative we can create any kind of marker that can be used to mark the function that should be used for output type conversion instead of `From::from` call. However this will implicitly grow delegation's syntax budget because it is equivalent to specifying output conversion explicitly: `reuse Trait::foo { self.0 } { MyStruct::delegation_from(self) }`.
Part of rust-lang/rust#118212.
r? @petrochenkov