Add `OwnedAnyUserData::take()`
This commit is contained in:
parent
3253ae8f4a
commit
bbd2488f79
|
@ -1152,6 +1152,14 @@ impl OwnedAnyUserData {
|
||||||
// Reattach lifetime to &self
|
// Reattach lifetime to &self
|
||||||
Ok(unsafe { mem::transmute::<RefMut<T>, RefMut<T>>(t) })
|
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.
|
/// Handle to a `UserData` metatable.
|
||||||
|
|
|
@ -831,11 +831,11 @@ fn test_owned_userdata() -> Result<()> {
|
||||||
|
|
||||||
assert_eq!(*ud.borrow::<&str>()?, "abc");
|
assert_eq!(*ud.borrow::<&str>()?, "abc");
|
||||||
*ud.borrow_mut()? = "cba";
|
*ud.borrow_mut()? = "cba";
|
||||||
assert_eq!(*ud.to_ref().borrow::<&str>()?, "cba");
|
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
ud.borrow::<i64>(),
|
ud.borrow::<i64>(),
|
||||||
Err(Error::UserDataTypeMismatch)
|
Err(Error::UserDataTypeMismatch)
|
||||||
));
|
));
|
||||||
|
assert_eq!(ud.take::<&str>()?, "cba");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue