Skip to content

Commit f0b0942

Browse files
Constify Deref and DerefMut
1 parent d05e8e8 commit f0b0942

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

core/src/ops/deref.rs

+47
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
#[doc(alias = "&*")]
134134
#[stable(feature = "rust1", since = "1.0.0")]
135135
#[rustc_diagnostic_item = "Deref"]
136+
#[cfg_attr(not(bootstrap), const_trait)]
136137
pub trait Deref {
137138
/// The resulting type after dereferencing.
138139
#[stable(feature = "rust1", since = "1.0.0")]
@@ -147,6 +148,7 @@ pub trait Deref {
147148
fn deref(&self) -> &Self::Target;
148149
}
149150

151+
#[cfg(bootstrap)]
150152
#[stable(feature = "rust1", since = "1.0.0")]
151153
impl<T: ?Sized> Deref for &T {
152154
type Target = T;
@@ -157,9 +159,21 @@ impl<T: ?Sized> Deref for &T {
157159
}
158160
}
159161

162+
#[cfg(not(bootstrap))]
163+
#[stable(feature = "rust1", since = "1.0.0")]
164+
impl<T: ?Sized> const Deref for &T {
165+
type Target = T;
166+
167+
#[rustc_diagnostic_item = "noop_method_deref"]
168+
fn deref(&self) -> &T {
169+
*self
170+
}
171+
}
172+
160173
#[stable(feature = "rust1", since = "1.0.0")]
161174
impl<T: ?Sized> !DerefMut for &T {}
162175

176+
#[cfg(bootstrap)]
163177
#[stable(feature = "rust1", since = "1.0.0")]
164178
impl<T: ?Sized> Deref for &mut T {
165179
type Target = T;
@@ -169,6 +183,16 @@ impl<T: ?Sized> Deref for &mut T {
169183
}
170184
}
171185

186+
#[cfg(not(bootstrap))]
187+
#[stable(feature = "rust1", since = "1.0.0")]
188+
impl<T: ?Sized> const Deref for &mut T {
189+
type Target = T;
190+
191+
fn deref(&self) -> &T {
192+
*self
193+
}
194+
}
195+
172196
/// Used for mutable dereferencing operations, like in `*v = 1;`.
173197
///
174198
/// In addition to being used for explicit dereferencing operations with the
@@ -258,23 +282,46 @@ impl<T: ?Sized> Deref for &mut T {
258282
/// *x = 'b';
259283
/// assert_eq!('b', x.value);
260284
/// ```
285+
#[cfg(not(bootstrap))]
286+
#[lang = "deref_mut"]
287+
#[doc(alias = "*")]
288+
#[stable(feature = "rust1", since = "1.0.0")]
289+
#[const_trait]
290+
pub trait DerefMut: ~const Deref {
291+
/// Mutably dereferences the value.
292+
#[stable(feature = "rust1", since = "1.0.0")]
293+
#[rustc_diagnostic_item = "deref_mut_method"]
294+
fn deref_mut(&mut self) -> &mut Self::Target;
295+
}
296+
297+
/// Bootstrap
261298
#[lang = "deref_mut"]
262299
#[doc(alias = "*")]
263300
#[stable(feature = "rust1", since = "1.0.0")]
301+
#[cfg(bootstrap)]
264302
pub trait DerefMut: Deref {
265303
/// Mutably dereferences the value.
266304
#[stable(feature = "rust1", since = "1.0.0")]
267305
#[rustc_diagnostic_item = "deref_mut_method"]
268306
fn deref_mut(&mut self) -> &mut Self::Target;
269307
}
270308

309+
#[cfg(bootstrap)]
271310
#[stable(feature = "rust1", since = "1.0.0")]
272311
impl<T: ?Sized> DerefMut for &mut T {
273312
fn deref_mut(&mut self) -> &mut T {
274313
*self
275314
}
276315
}
277316

317+
#[cfg(not(bootstrap))]
318+
#[stable(feature = "rust1", since = "1.0.0")]
319+
impl<T: ?Sized> const DerefMut for &mut T {
320+
fn deref_mut(&mut self) -> &mut T {
321+
*self
322+
}
323+
}
324+
278325
/// Perma-unstable marker trait. Indicates that the type has a well-behaved [`Deref`]
279326
/// (and, if applicable, [`DerefMut`]) implementation. This is relied on for soundness
280327
/// of deref patterns.

0 commit comments

Comments
 (0)