macro_rules! cf_retained {
($ty:ty, field = $field:ident, retain = $retain:path, release = $release:path, drop = null_out $(,)?) => {
impl Clone for $ty {
fn clone(&self) -> Self {
Self {
$field: unsafe { $retain(self.$field) },
}
}
}
impl Drop for $ty {
fn drop(&mut self) {
if !self.$field.is_null() {
unsafe {
$release(self.$field);
}
self.$field = ::core::ptr::null_mut();
}
}
}
};
($ty:ty, field = $field:ident, retain = $retain:path, release = $release:path, drop = unchecked $(,)?) => {
impl Clone for $ty {
fn clone(&self) -> Self {
Self {
$field: unsafe { $retain(self.$field) },
}
}
}
impl Drop for $ty {
fn drop(&mut self) {
unsafe {
$release(self.$field);
}
}
}
};
($ty:ty, field = $field:ident, retain = $retain:path, release = $release:path $(,)?) => {
impl Clone for $ty {
fn clone(&self) -> Self {
Self {
$field: unsafe { $retain(self.$field) },
}
}
}
impl Drop for $ty {
fn drop(&mut self) {
if !self.$field.is_null() {
unsafe {
$release(self.$field);
}
}
}
}
};
($ty:ty, retain = $retain:path, release = $release:path, drop = unchecked $(,)?) => {
impl Clone for $ty {
fn clone(&self) -> Self {
Self(unsafe { $retain(self.0) })
}
}
impl Drop for $ty {
fn drop(&mut self) {
unsafe {
$release(self.0);
}
}
}
};
($ty:ty, retain = $retain:path, release = $release:path $(,)?) => {
impl Clone for $ty {
fn clone(&self) -> Self {
Self(unsafe { $retain(self.0) })
}
}
impl Drop for $ty {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe {
$release(self.0);
}
}
}
}
};
}
pub(crate) use cf_retained;