/// Custom code to free a handle. /// /// This is similar to the [`Drop`] trait, and may be used to implement [`Drop`], but allows handles /// to be dropped depending on context. pubtrait Free { /// Calls the handle's free function. /// /// # Safety /// The handle must be owned by the caller and safe to free. unsafefn free(&mutself);
}
/// A wrapper to provide ownership for handles to automatically drop via the handle's [`Free`] trait. #[repr(transparent)] #[derive(PartialEq, Eq, Default, Debug)] pubstruct Owned<T: Free>(T);
impl<T: Free> Owned<T> { /// Takes ownership of the handle. /// /// # Safety /// /// The handle must be owned by the caller and safe to free. pubunsafefn new(x: T) -> Self { Self(x)
}
}
impl<T: Free> Drop for Owned<T> { fn drop(&mutself) { unsafe { self.0.free() };
}
}
impl<T: Free> core::ops::Deref for Owned<T> { type Target = T; fn deref(&self) -> &Self::Target {
&self.0
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.