mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2024-11-23 23:29:50 +00:00
Add UniquePtr::pin_mut
This commit is contained in:
parent
227ff0cbf5
commit
09a3086b45
@ -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).
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user