Skip to content

Commit bebfcfd

Browse files
ridiculousfishPeter Ammon
authored and
Peter Ammon
committed
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 46176eb commit bebfcfd

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

library/core/src/option.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
135135
use crate::pin::Pin;
136136
use crate::{
137-
convert, fmt, hint, mem,
138-
ops::{self, Deref, DerefMut},
137+
convert, fmt, hint, mem, ptr,
138+
ops::{self, Deref, DerefMut, Range},
139139
};
140140

141141
/// The `Option` type. See [the module level documentation](self) for more.
@@ -1257,6 +1257,16 @@ impl<T: Copy> Clone for Option<T> {
12571257
}
12581258
}
12591259

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

12611271
#[stable(feature = "rust1", since = "1.0.0")]
12621272
impl<T> Default for Option<T> {

0 commit comments

Comments
 (0)