Skip to content

Commit 09c40e5

Browse files
Optimize Option<Range>::clone
Range<T> is not Copy even if T is Copy, which pessimizes the implementation of Option<Range>::clone. Specialize Option::clone for Range so that it can be more efficient. The specialization uses ptr::read to emulate Copy.
1 parent 7a00c27 commit 09c40e5

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

library/core/src/option.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ use crate::iter::{FromIterator, FusedIterator, TrustedLen};
135135
use crate::pin::Pin;
136136
use crate::{
137137
convert, fmt, hint, mem,
138-
ops::{self, Deref, DerefMut},
138+
ops::{self, Deref, DerefMut, Range},
139+
ptr,
139140
};
140141

141142
/// The `Option` type. See [the module level documentation](self) for more.
@@ -1257,6 +1258,17 @@ impl<T: Copy> Clone for Option<T> {
12571258
}
12581259
}
12591260

1261+
// Range<T> is not Copy even if T is copy (see #27186),
1262+
// so provide an efficient implementation.
1263+
#[stable(feature = "rust1", since = "1.0.0")]
1264+
impl<T: Copy> Clone for Option<Range<T>> {
1265+
#[inline]
1266+
fn clone(&self) -> Self {
1267+
// SAFETY: 'self' is not Drop so memcpy is OK.
1268+
unsafe { ptr::read(self as *const Self) }
1269+
}
1270+
}
1271+
12601272
#[stable(feature = "rust1", since = "1.0.0")]
12611273
impl<T> Default for Option<T> {
12621274
/// Returns [`None`][Option::None].

0 commit comments

Comments
 (0)