diff options
| author | Jonas Schievink <jonasschievink@gmail.com> | 2021-01-31 01:47:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-31 01:47:23 +0100 |
| commit | 054c29d22ca171ea8565cc5c53dbbf0d473f05eb (patch) | |
| tree | 89fb3259b4c4747e499b0b73176c5908dbd56f1d /src/test/codegen | |
| parent | b28a1b29511ebbc13021165a96163d32a98bb7e7 (diff) | |
| parent | bef4ec2fc51aa020fe6bf81257459f096763f3b7 (diff) | |
| download | rust-054c29d22ca171ea8565cc5c53dbbf0d473f05eb.tar.gz rust-054c29d22ca171ea8565cc5c53dbbf0d473f05eb.zip | |
Rollup merge of #80279 - Yaulendil:str-as-mut, r=m-ou-se
Implement missing `AsMut<str>` for `str`
Allows `&mut str` to be taken by a Generic which requires `T` such that `T: AsMut<str>`. Motivating example:
```rust
impl<'i, T> From<T> for StructImmut<'i> where
T: AsRef<str> + 'i,
{
fn from(asref: T) -> Self {
let string: &str = asref.as_ref();
// ...
}
}
impl<'i, T> From<T> for StructMut<'i> where
T: AsMut<str> + 'i,
{
fn from(mut asmut: T) -> Self {
let string: &mut str = asmut.as_mut();
// ...
}
}
```
The Immutable form of this structure can be constructed by `StructImmut::from(s)` where `s` may be a `&String` or a `&str`, because `AsRef<str>` is implemented for `str`. However, the mutable form of the structure can be constructed in the same way **only** with a `&mut String`, and **not** with a `&mut str`.
This change does have some precedent, because as can be seen in [the Implementors](https://doc.rust-lang.org/std/convert/trait.AsMut.html#implementors), `AsMut<[T]>` is implemented for `[T]` as well as for `Vec<T>`, but `AsMut<str>` is implemented only for `String`. This would complete the symmetry.
As a trait implementation, this should be immediately stable.
Diffstat (limited to 'src/test/codegen')
0 files changed, 0 insertions, 0 deletions
