Skip to content

Commit 75f8015

Browse files
committed
Auto merge of rust-lang#92560 - matthiaskrgr:rollup-jeli7ip, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#91587 (core::ops::unsize: improve docs for DispatchFromDyn) - rust-lang#91907 (Allow `_` as the length of array types and repeat expressions) - rust-lang#92515 (RustWrapper: adapt for an LLVM API change) - rust-lang#92516 (Do not use deprecated -Zsymbol-mangling-version in bootstrap) - rust-lang#92530 (Move `contains` method of Option and Result lower in docs) - rust-lang#92546 (Update books) - rust-lang#92551 (rename StackPopClean::None to Root) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents bbfc8fd + 19389c8 commit 75f8015

File tree

3 files changed

+124
-89
lines changed

3 files changed

+124
-89
lines changed

core/src/ops/unsize.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,38 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
6868
#[unstable(feature = "coerce_unsized", issue = "27732")]
6969
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
7070

71-
/// This is used for object safety, to check that a method's receiver type can be dispatched on.
71+
/// `DispatchFromDyn` is used in the implementation of object safety checks (specifically allowing
72+
/// arbitrary self types), to guarantee that a method's receiver type can be dispatched on.
73+
///
74+
/// Note: `DispatchFromDyn` was briefly named `CoerceSized` (and had a slightly different
75+
/// interpretation).
76+
///
77+
/// Imagine we have a trait object `t` with type `&dyn Tr`, where `Tr` is some trait with a method
78+
/// `m` defined as `fn m(&self);`. When calling `t.m()`, the receiver `t` is a wide pointer, but an
79+
/// implementation of `m` will expect a narrow pointer as `&self` (a reference to the concrete
80+
/// type). The compiler must generate an implicit conversion from the trait object/wide pointer to
81+
/// the concrete reference/narrow pointer. Implementing `DispatchFromDyn` indicates that that
82+
/// conversion is allowed and thus that the type implementing `DispatchFromDyn` is safe to use as
83+
/// the self type in an object-safe method. (in the above example, the compiler will require
84+
/// `DispatchFromDyn` is implemented for `&'a U`).
85+
///
86+
/// `DispatchFromDyn` does not specify the conversion from wide pointer to narrow pointer; the
87+
/// conversion is hard-wired into the compiler. For the conversion to work, the following
88+
/// properties must hold (i.e., it is only safe to implement `DispatchFromDyn` for types which have
89+
/// these properties, these are also checked by the compiler):
90+
///
91+
/// * EITHER `Self` and `T` are either both references or both raw pointers; in either case, with
92+
/// the same mutability.
93+
/// * OR, all of the following hold
94+
/// - `Self` and `T` must have the same type constructor, and only vary in a single type parameter
95+
/// formal (the *coerced type*, e.g., `impl DispatchFromDyn<Rc<T>> for Rc<U>` is ok and the
96+
/// single type parameter (instantiated with `T` or `U`) is the coerced type,
97+
/// `impl DispatchFromDyn<Arc<T>> for Rc<U>` is not ok).
98+
/// - The definition for `Self` must be a struct.
99+
/// - The definition for `Self` must not be `#[repr(packed)]` or `#[repr(C)]`.
100+
/// - Other than one-aligned, zero-sized fields, the definition for `Self` must have exactly one
101+
/// field and that field's type must be the coerced type. Furthermore, `Self`'s field type must
102+
/// implement `DispatchFromDyn<F>` where `F` is the type of `T`'s field type.
72103
///
73104
/// An example implementation of the trait:
74105
///

core/src/option.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -571,36 +571,6 @@ impl<T> Option<T> {
571571
!self.is_some()
572572
}
573573

574-
/// Returns `true` if the option is a [`Some`] value containing the given value.
575-
///
576-
/// # Examples
577-
///
578-
/// ```
579-
/// #![feature(option_result_contains)]
580-
///
581-
/// let x: Option<u32> = Some(2);
582-
/// assert_eq!(x.contains(&2), true);
583-
///
584-
/// let x: Option<u32> = Some(3);
585-
/// assert_eq!(x.contains(&2), false);
586-
///
587-
/// let x: Option<u32> = None;
588-
/// assert_eq!(x.contains(&2), false);
589-
/// ```
590-
#[must_use]
591-
#[inline]
592-
#[unstable(feature = "option_result_contains", issue = "62358")]
593-
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
594-
pub const fn contains<U>(&self, x: &U) -> bool
595-
where
596-
U: ~const PartialEq<T>,
597-
{
598-
match self {
599-
Some(y) => x.eq(y),
600-
None => false,
601-
}
602-
}
603-
604574
/////////////////////////////////////////////////////////////////////////
605575
// Adapter for working with references
606576
/////////////////////////////////////////////////////////////////////////
@@ -1573,6 +1543,36 @@ impl<T> Option<T> {
15731543
mem::replace(self, Some(value))
15741544
}
15751545

1546+
/// Returns `true` if the option is a [`Some`] value containing the given value.
1547+
///
1548+
/// # Examples
1549+
///
1550+
/// ```
1551+
/// #![feature(option_result_contains)]
1552+
///
1553+
/// let x: Option<u32> = Some(2);
1554+
/// assert_eq!(x.contains(&2), true);
1555+
///
1556+
/// let x: Option<u32> = Some(3);
1557+
/// assert_eq!(x.contains(&2), false);
1558+
///
1559+
/// let x: Option<u32> = None;
1560+
/// assert_eq!(x.contains(&2), false);
1561+
/// ```
1562+
#[must_use]
1563+
#[inline]
1564+
#[unstable(feature = "option_result_contains", issue = "62358")]
1565+
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
1566+
pub const fn contains<U>(&self, x: &U) -> bool
1567+
where
1568+
U: ~const PartialEq<T>,
1569+
{
1570+
match self {
1571+
Some(y) => x.eq(y),
1572+
None => false,
1573+
}
1574+
}
1575+
15761576
/// Zips `self` with another `Option`.
15771577
///
15781578
/// If `self` is `Some(s)` and `other` is `Some(o)`, this method returns `Some((s, o))`.

core/src/result.rs

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -563,64 +563,6 @@ impl<T, E> Result<T, E> {
563563
!self.is_ok()
564564
}
565565

566-
/// Returns `true` if the result is an [`Ok`] value containing the given value.
567-
///
568-
/// # Examples
569-
///
570-
/// ```
571-
/// #![feature(option_result_contains)]
572-
///
573-
/// let x: Result<u32, &str> = Ok(2);
574-
/// assert_eq!(x.contains(&2), true);
575-
///
576-
/// let x: Result<u32, &str> = Ok(3);
577-
/// assert_eq!(x.contains(&2), false);
578-
///
579-
/// let x: Result<u32, &str> = Err("Some error message");
580-
/// assert_eq!(x.contains(&2), false);
581-
/// ```
582-
#[must_use]
583-
#[inline]
584-
#[unstable(feature = "option_result_contains", issue = "62358")]
585-
pub fn contains<U>(&self, x: &U) -> bool
586-
where
587-
U: PartialEq<T>,
588-
{
589-
match self {
590-
Ok(y) => x == y,
591-
Err(_) => false,
592-
}
593-
}
594-
595-
/// Returns `true` if the result is an [`Err`] value containing the given value.
596-
///
597-
/// # Examples
598-
///
599-
/// ```
600-
/// #![feature(result_contains_err)]
601-
///
602-
/// let x: Result<u32, &str> = Ok(2);
603-
/// assert_eq!(x.contains_err(&"Some error message"), false);
604-
///
605-
/// let x: Result<u32, &str> = Err("Some error message");
606-
/// assert_eq!(x.contains_err(&"Some error message"), true);
607-
///
608-
/// let x: Result<u32, &str> = Err("Some other error message");
609-
/// assert_eq!(x.contains_err(&"Some error message"), false);
610-
/// ```
611-
#[must_use]
612-
#[inline]
613-
#[unstable(feature = "result_contains_err", issue = "62358")]
614-
pub fn contains_err<F>(&self, f: &F) -> bool
615-
where
616-
F: PartialEq<E>,
617-
{
618-
match self {
619-
Ok(_) => false,
620-
Err(e) => f == e,
621-
}
622-
}
623-
624566
/////////////////////////////////////////////////////////////////////////
625567
// Adapter for each variant
626568
/////////////////////////////////////////////////////////////////////////
@@ -1491,6 +1433,68 @@ impl<T, E> Result<T, E> {
14911433
Err(e) => e,
14921434
}
14931435
}
1436+
1437+
/////////////////////////////////////////////////////////////////////////
1438+
// Misc or niche
1439+
/////////////////////////////////////////////////////////////////////////
1440+
1441+
/// Returns `true` if the result is an [`Ok`] value containing the given value.
1442+
///
1443+
/// # Examples
1444+
///
1445+
/// ```
1446+
/// #![feature(option_result_contains)]
1447+
///
1448+
/// let x: Result<u32, &str> = Ok(2);
1449+
/// assert_eq!(x.contains(&2), true);
1450+
///
1451+
/// let x: Result<u32, &str> = Ok(3);
1452+
/// assert_eq!(x.contains(&2), false);
1453+
///
1454+
/// let x: Result<u32, &str> = Err("Some error message");
1455+
/// assert_eq!(x.contains(&2), false);
1456+
/// ```
1457+
#[must_use]
1458+
#[inline]
1459+
#[unstable(feature = "option_result_contains", issue = "62358")]
1460+
pub fn contains<U>(&self, x: &U) -> bool
1461+
where
1462+
U: PartialEq<T>,
1463+
{
1464+
match self {
1465+
Ok(y) => x == y,
1466+
Err(_) => false,
1467+
}
1468+
}
1469+
1470+
/// Returns `true` if the result is an [`Err`] value containing the given value.
1471+
///
1472+
/// # Examples
1473+
///
1474+
/// ```
1475+
/// #![feature(result_contains_err)]
1476+
///
1477+
/// let x: Result<u32, &str> = Ok(2);
1478+
/// assert_eq!(x.contains_err(&"Some error message"), false);
1479+
///
1480+
/// let x: Result<u32, &str> = Err("Some error message");
1481+
/// assert_eq!(x.contains_err(&"Some error message"), true);
1482+
///
1483+
/// let x: Result<u32, &str> = Err("Some other error message");
1484+
/// assert_eq!(x.contains_err(&"Some error message"), false);
1485+
/// ```
1486+
#[must_use]
1487+
#[inline]
1488+
#[unstable(feature = "result_contains_err", issue = "62358")]
1489+
pub fn contains_err<F>(&self, f: &F) -> bool
1490+
where
1491+
F: PartialEq<E>,
1492+
{
1493+
match self {
1494+
Ok(_) => false,
1495+
Err(e) => f == e,
1496+
}
1497+
}
14941498
}
14951499

14961500
impl<T, E> Result<&T, E> {

0 commit comments

Comments
 (0)