Skip to content

Commit

Permalink
refactor: use the default implementation of clone_from in `impl con…
Browse files Browse the repository at this point in the history
…st Clone`

`Clone::clone_from` gained `#[default_method_body_is_const]` in
[rust-lang/rust#91804][1].

n.b. `vec.rs` is shared by `r3_core` and `r3_kernel` (via a symbolic
link), hence the lack of a scope in the commit message headline.

[1]: rust-lang/rust#91804
  • Loading branch information
yvt committed Mar 21, 2022
1 parent 79e4d28 commit 388d3ce
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 49 deletions.
24 changes: 0 additions & 24 deletions doc/toolchain_limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -632,30 +632,6 @@ const _: () = assert!(matches!((2..4).next(), Some(2)));
```


### `[tag:clone_from_default]` `Clone::clone_from` lacks `#[default_method_body_is_const]`

When implementing `const Clone`, you can't use the provided implementation of `Clone::clone_from`.

```rust
#![feature(const_trait_impl)]
#![feature(const_mut_refs)]
struct A;
impl const Clone for A {
fn clone(&self) -> Self { A }
fn clone_from(&mut self, source: &Self) {}
}
```

```rust,compile_fail
#![feature(const_trait_impl)]
struct A;
impl const Clone for A {
fn clone(&self) -> Self { A }
// error: const trait implementations may not use non-const default functions
}
```


### `[tag:iterator_const_default]` `Iterator`'s methods lack `#[default_method_body_is_const]`

Implementing `const Iterator` requires you to implement all of its methods, which is impossible to do correctly.
Expand Down
12 changes: 0 additions & 12 deletions src/r3_core/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,6 @@ impl<System, T> const Clone for Bind<'_, System, T> {
fn clone(&self) -> Self {
*self
}

// `clone_from` needed due to [ref:clone_from_default]
#[inline]
fn clone_from(&mut self, source: &Self) {
*self = *source;
}
}

/// A [binder][1] that gives `&T` to a bound function.
Expand Down Expand Up @@ -296,12 +290,6 @@ impl<System, T> const Clone for BindRef<System, T> {
fn clone(&self) -> Self {
*self
}

// `clone_from` needed due to [ref:clone_from_default]
#[inline]
fn clone_from(&mut self, source: &Self) {
*self = *source;
}
}

// `BindDefiner` doesn't contain `T`, so this `impl` must use a concrete `T`
Expand Down
4 changes: 0 additions & 4 deletions src/r3_core/src/utils/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ impl const Clone for ConstAllocator {
ref_count: self.ref_count,
}
}

fn clone_from(&mut self, source: &Self) {
*self = source.clone();
}
}

impl const Drop for ConstAllocator {
Expand Down
5 changes: 0 additions & 5 deletions src/r3_core/src/utils/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ impl<T: Copy> const Clone for Frozen<T> {
// Don't use `T as Clone` because it could expose interior mutability.
*self
}

#[inline]
fn clone_from(&mut self, source: &Self) {
*self = *source;
}
}

impl<T: Copy + ~const fmt::Debug> const fmt::Debug for Frozen<T> {
Expand Down
4 changes: 0 additions & 4 deletions src/r3_core/src/utils/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ impl<T: ~const Clone + ~const Drop> const Clone for ComptimeVec<T> {
}
self.map(clone_shim)
}

fn clone_from(&mut self, source: &Self) {
*self = source.clone();
}
}

impl<T> const Drop for ComptimeVec<T>
Expand Down

0 comments on commit 388d3ce

Please sign in to comment.