/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Objective-C bindings and helpers.
// Forward all exports from the `objc` crate. pubuse objc::*;
/// An objc class instance which contains rust data `T`. #[derive(Clone, Copy)] #[repr(transparent)] pubstruct Objc<T> { pub instance: cocoa::id,
_phantom: std::marker::PhantomData<*mut T>,
}
impl<T> std::ops::Deref for StrongRef<T> { type Target = T; fn deref(&self) -> &Self::Target { let obj: &cocoa::id = &*self.ptr; unsafe { std::mem::transmute(obj) }
}
}
impl<T> StrongRef<T> { /// Assume the given pointer-wrapper is an already-retained strong reference. /// /// # Safety /// The type _must_ be the same size as cocoa::id and contain only a cocoa::id. pubunsafefn new(v: T) -> Self {
std::mem::transmute_copy(&v)
}
/// Retain the given pointer-wrapper. /// /// # Safety /// The type _must_ be the same size as cocoa::id and contain only a cocoa::id. #[allow(dead_code)] pubunsafefn retain(v: T) -> Self { let obj: cocoa::id = std::mem::transmute_copy(&v);
StrongRef {
ptr: rc::StrongPtr::retain(obj),
_phantom: std::marker::PhantomData,
}
}
pubfn autorelease(self) -> T { let obj = self.ptr.autorelease(); unsafe { std::mem::transmute_copy(&obj) }
}
/// Unwrap the StrongRef value without affecting reference counts. /// /// This is the opposite of `new`. #[allow(dead_code)] pubfn unwrap(self: Self) -> T { let v = unsafe { std::mem::transmute_copy(&self) };
std::mem::forget(self);
v
}
/// Cast to a base class. /// /// Bindgen pointer-wrappers have trival `From<Derived> for Base` implementations. pubfn cast<U: From<T>>(self) -> StrongRef<U> {
StrongRef {
ptr: self.ptr,
_phantom: std::marker::PhantomData,
}
}
}
/// A weak objective-c reference to `T`. #[derive(Clone)] #[repr(transparent)] pubstruct WeakRef<T> {
ptr: rc::WeakPtr,
_phantom: std::marker::PhantomData<T>,
}
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.