Rollup merge of #62250 - czipperz:improve-box-clone-doctests, r=GuillaumeGomez

Improve box clone doctests to ensure the documentation is valid
This commit is contained in:
Mark Rousskov 2019-07-03 09:59:24 -04:00 committed by GitHub
commit 8ca4a6a486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 1 deletions

View File

@ -367,12 +367,19 @@ impl<T: Clone> Clone for Box<T> {
/// ``` /// ```
/// let x = Box::new(5); /// let x = Box::new(5);
/// let y = x.clone(); /// let y = x.clone();
///
/// // The value is the same
/// assert_eq!(x, y);
///
/// // But they are unique objects
/// assert_ne!(&*x as *const i32, &*y as *const i32);
/// ``` /// ```
#[rustfmt::skip] #[rustfmt::skip]
#[inline] #[inline]
fn clone(&self) -> Box<T> { fn clone(&self) -> Box<T> {
box { (**self).clone() } box { (**self).clone() }
} }
/// Copies `source`'s contents into `self` without creating a new allocation. /// Copies `source`'s contents into `self` without creating a new allocation.
/// ///
/// # Examples /// # Examples
@ -380,10 +387,15 @@ impl<T: Clone> Clone for Box<T> {
/// ``` /// ```
/// let x = Box::new(5); /// let x = Box::new(5);
/// let mut y = Box::new(10); /// let mut y = Box::new(10);
/// let yp: *const i32 = &*y;
/// ///
/// y.clone_from(&x); /// y.clone_from(&x);
/// ///
/// assert_eq!(*y, 5); /// // The value is the same
/// assert_eq!(x, y);
///
/// // And no allocation occurred
/// assert_eq!(yp, &*y);
/// ``` /// ```
#[inline] #[inline]
fn clone_from(&mut self, source: &Box<T>) { fn clone_from(&mut self, source: &Box<T>) {