Skip to content

Commit

Permalink
Add ClassBuilder::add_static_ivar
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 1, 2022
1 parent 8b9df64 commit 9c9a63e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
(default) feature flag `"foundation"`.
* Added `declare_class!`, `extern_class!` and `ns_string!` macros from
`objc2-foundation`.
* Added helper method `ClassBuilder::add_static_ivar`.

### Changed
* **BREAKING**: Change selector syntax in `declare_class!` macro to be more Rust-like.
Expand Down
2 changes: 1 addition & 1 deletion objc2/examples/class_with_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'a> MyObject<'a> {
let superclass = NSObject::class();
let mut builder = ClassBuilder::new("MyObject", superclass).unwrap();

builder.add_ivar::<<NumberIvar<'a> as IvarType>::Type>(<NumberIvar<'a>>::NAME);
builder.add_static_ivar::<NumberIvar<'a>>();

/// Helper struct since we can't access the instance variable
/// from inside MyObject, since it hasn't been initialized yet!
Expand Down
9 changes: 9 additions & 0 deletions objc2/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,15 @@ impl ClassBuilder {
assert!(success.as_bool(), "Failed to add ivar {}", name);
}

/// Adds an ivar with type `T` and the provided name.
///
/// # Panics
///
/// If the ivar wasn't successfully added.
pub fn add_static_ivar<T: IvarType>(&mut self) {
self.add_ivar::<T::Type>(T::NAME);
}

/// Adds the given protocol to self.
///
/// # Panics
Expand Down
8 changes: 6 additions & 2 deletions objc2/src/declare/ivar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ unsafe impl<T: IvarType> IvarType for MaybeUninit<T> {
/// particular, this is never safe to have on the stack by itself.
///
/// Additionally, the instance variable described by `T` must be available on
/// the specific instance, and be of the exact same type.
/// the specific instance, and be of the exact same type. When declaring the
/// object yourself, you can ensure this using
/// [`ClassBuilder::add_static_ivar`].
///
/// Finally, two ivars with the same name must not be used on the same object.
///
/// [`ClassBuilder::add_static_ivar`]: crate::declare::ClassBuilder::add_static_ivar
///
///
/// # Examples
///
Expand Down Expand Up @@ -106,7 +110,7 @@ unsafe impl<T: IvarType> IvarType for MaybeUninit<T> {
/// # use objc2::declare::ClassBuilder;
/// # let mut builder = ClassBuilder::new("MyObject", class!(NSObject)).unwrap();
/// // Declare the class and add the instance variable to it
/// builder.add_ivar::<<MyCustomIvar as IvarType>::Type>(MyCustomIvar::NAME);
/// builder.add_static_ivar::<MyCustomIvar>();
/// # let _cls = builder.register();
///
/// let obj: MyObject;
Expand Down
8 changes: 3 additions & 5 deletions objc2/src/macros/declare_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,7 @@ macro_rules! declare_class {
unsafe $v struct $name<>: $inherits, $($inheritance_rest,)* $crate::runtime::Object {
// SAFETY:
// - The ivars are in a type used as an Objective-C object.
// - The instance variable is defined in the exact same manner
// in `class` below.
// - The ivar is added to the class below.
// - Rust prevents having two fields with the same name.
// - Caller upholds that the ivars are properly initialized.
$($ivar_v $ivar: $crate::declare::Ivar<$ivar>,)*
Expand Down Expand Up @@ -527,10 +526,9 @@ macro_rules! declare_class {
);
let mut builder = $crate::declare::ClassBuilder::new(stringify!($name), superclass).expect(err_str);

// Ivars
$(
builder.add_ivar::<<$ivar as $crate::declare::IvarType>::Type>(
<$ivar as $crate::declare::IvarType>::NAME
);
builder.add_static_ivar::<$ivar>();
)*

$(
Expand Down

0 comments on commit 9c9a63e

Please sign in to comment.