|
@@ -463,90 +463,7 @@ unsafe fn unsafe_cast<'a, U: ObjectType>(self) -> &'a U
|
|
impl<T: ObjectType> ObjectDeref for &T {}
|
|
impl<T: ObjectType> ObjectDeref for &T {}
|
|
impl<T: ObjectType> ObjectCast for &T {}
|
|
impl<T: ObjectType> ObjectCast for &T {}
|
|
|
|
|
|
-/// Trait for mutable type casting operations in the QOM hierarchy.
|
|
|
|
-///
|
|
|
|
-/// This trait provides the mutable counterparts to [`ObjectCast`]'s conversion
|
|
|
|
-/// functions. Unlike `ObjectCast`, this trait returns `Result` for fallible
|
|
|
|
-/// conversions to preserve the original smart pointer if the cast fails. This
|
|
|
|
-/// is necessary because mutable references cannot be copied, so a failed cast
|
|
|
|
-/// must return ownership of the original reference. For example:
|
|
|
|
-///
|
|
|
|
-/// ```ignore
|
|
|
|
-/// let mut dev = get_device();
|
|
|
|
-/// // If this fails, we need the original `dev` back to try something else
|
|
|
|
-/// match dev.dynamic_cast_mut::<FooDevice>() {
|
|
|
|
-/// Ok(foodev) => /* use foodev */,
|
|
|
|
-/// Err(dev) => /* still have ownership of dev */
|
|
|
|
-/// }
|
|
|
|
-/// ```
|
|
|
|
-pub trait ObjectCastMut: Sized + ObjectDeref + DerefMut
|
|
|
|
-where
|
|
|
|
- Self::Target: ObjectType,
|
|
|
|
-{
|
|
|
|
- /// Safely convert from a derived type to one of its parent types.
|
|
|
|
- ///
|
|
|
|
- /// This is always safe; the [`IsA`] trait provides static verification
|
|
|
|
- /// that `Self` dereferences to `U` or a child of `U`.
|
|
|
|
- fn upcast_mut<'a, U: ObjectType>(self) -> &'a mut U
|
|
|
|
- where
|
|
|
|
- Self::Target: IsA<U>,
|
|
|
|
- Self: 'a,
|
|
|
|
- {
|
|
|
|
- // SAFETY: soundness is declared via IsA<U>, which is an unsafe trait
|
|
|
|
- unsafe { self.unsafe_cast_mut::<U>() }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Attempt to convert to a derived type.
|
|
|
|
- ///
|
|
|
|
- /// Returns `Ok(..)` if the object is of type `U`, or `Err(self)` if the
|
|
|
|
- /// object if the conversion failed. This is verified at runtime by
|
|
|
|
- /// checking the object's type information.
|
|
|
|
- fn downcast_mut<'a, U: IsA<Self::Target>>(self) -> Result<&'a mut U, Self>
|
|
|
|
- where
|
|
|
|
- Self: 'a,
|
|
|
|
- {
|
|
|
|
- self.dynamic_cast_mut::<U>()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Attempt to convert between any two types in the QOM hierarchy.
|
|
|
|
- ///
|
|
|
|
- /// Returns `Ok(..)` if the object is of type `U`, or `Err(self)` if the
|
|
|
|
- /// object if the conversion failed. This is verified at runtime by
|
|
|
|
- /// checking the object's type information.
|
|
|
|
- fn dynamic_cast_mut<'a, U: ObjectType>(self) -> Result<&'a mut U, Self>
|
|
|
|
- where
|
|
|
|
- Self: 'a,
|
|
|
|
- {
|
|
|
|
- unsafe {
|
|
|
|
- // SAFETY: upcasting to Object is always valid, and the
|
|
|
|
- // return type is either NULL or the argument itself
|
|
|
|
- let result: *mut U =
|
|
|
|
- object_dynamic_cast(self.as_object_mut_ptr(), U::TYPE_NAME.as_ptr()).cast();
|
|
|
|
-
|
|
|
|
- result.as_mut().ok_or(self)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Convert to any QOM type without verification.
|
|
|
|
- ///
|
|
|
|
- /// # Safety
|
|
|
|
- ///
|
|
|
|
- /// What safety? You need to know yourself that the cast is correct; only
|
|
|
|
- /// use when performance is paramount. It is still better than a raw
|
|
|
|
- /// pointer `cast()`, which does not even check that you remain in the
|
|
|
|
- /// realm of QOM `ObjectType`s.
|
|
|
|
- ///
|
|
|
|
- /// `unsafe_cast::<Object>()` is always safe.
|
|
|
|
- unsafe fn unsafe_cast_mut<'a, U: ObjectType>(self) -> &'a mut U
|
|
|
|
- where
|
|
|
|
- Self: 'a,
|
|
|
|
- {
|
|
|
|
- unsafe { &mut *self.as_mut_ptr::<Self::Target>().cast::<U>() }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
impl<T: ObjectType> ObjectDeref for &mut T {}
|
|
impl<T: ObjectType> ObjectDeref for &mut T {}
|
|
-impl<T: ObjectType> ObjectCastMut for &mut T {}
|
|
|
|
|
|
|
|
/// Trait a type must implement to be registered with QEMU.
|
|
/// Trait a type must implement to be registered with QEMU.
|
|
pub trait ObjectImpl: ObjectType + IsA<Object> {
|
|
pub trait ObjectImpl: ObjectType + IsA<Object> {
|