Add UniquePtr::pin_mut

This commit is contained in:
David Tolnay 2020-11-22 12:01:57 -08:00
parent 227ff0cbf5
commit 09a3086b45
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 16 additions and 6 deletions

View File

@ -68,6 +68,19 @@ where
}
}
/// Returns a mutable pinned reference to the object owned by this
/// UniquePtr.
///
/// # Panics
///
/// Panics if the UniquePtr holds a null pointer.
pub fn pin_mut(&mut self) -> Pin<&mut T> {
match self.as_mut() {
Some(target) => target,
None => panic!("called pin_mut on a null UniquePtr<{}>", T::__NAME),
}
}
/// Consumes the UniquePtr, releasing its ownership of the heap-allocated T.
///
/// Matches the behavior of [std::unique_ptr\<T\>::release](https://en.cppreference.com/w/cpp/memory/unique_ptr/release).

View File

@ -188,14 +188,11 @@ fn test_c_method_calls() {
let old_value = unique_ptr.get();
assert_eq!(2020, old_value);
assert_eq!(2021, unique_ptr.as_mut().unwrap().set(2021));
assert_eq!(2021, unique_ptr.pin_mut().set(2021));
assert_eq!(2021, unique_ptr.get());
assert_eq!(2021, unique_ptr.get2());
assert_eq!(
2022,
unique_ptr.as_mut().unwrap().set_succeed(2022).unwrap(),
);
assert!(unique_ptr.as_mut().unwrap().get_fail().is_err());
assert_eq!(2022, unique_ptr.pin_mut().set_succeed(2022).unwrap(),);
assert!(unique_ptr.pin_mut().get_fail().is_err());
assert_eq!(2021, ffi::Shared { z: 0 }.c_method_on_shared());
}