Add `OwnedAnyUserData::take()`

This commit is contained in:
Alex Orlenko 2023-05-03 23:29:33 +01:00
parent 3253ae8f4a
commit bbd2488f79
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
2 changed files with 9 additions and 1 deletions

View File

@ -1152,6 +1152,14 @@ impl OwnedAnyUserData {
// Reattach lifetime to &self
Ok(unsafe { mem::transmute::<RefMut<T>, RefMut<T>>(t) })
}
/// Takes the value out of this userdata.
///
/// This is a shortcut for [`AnyUserData::take()`]
#[inline]
pub fn take<T: 'static>(&self) -> Result<T> {
self.to_ref().take()
}
}
/// Handle to a `UserData` metatable.

View File

@ -831,11 +831,11 @@ fn test_owned_userdata() -> Result<()> {
assert_eq!(*ud.borrow::<&str>()?, "abc");
*ud.borrow_mut()? = "cba";
assert_eq!(*ud.to_ref().borrow::<&str>()?, "cba");
assert!(matches!(
ud.borrow::<i64>(),
Err(Error::UserDataTypeMismatch)
));
assert_eq!(ud.take::<&str>()?, "cba");
Ok(())
}