Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better follow API guidelines #102

Merged
merged 12 commits into from
Jan 2, 2022
Next Next commit
Rename Object::get_[mut]_ivar methods
Follow guideline: Getter names follow Rust convention (C-GETTER)
madsmtm committed Dec 22, 2021

Verified

This commit was signed with the committer’s verified signature. The key has expired.
tvdeyen Thomas von Deyen
commit 2cbf5b4a4cc9cca601e4f1f1b33e076bdaa888a9
4 changes: 2 additions & 2 deletions objc2-foundation/examples/class_with_lifetime.rs
Original file line number Diff line number Diff line change
@@ -35,14 +35,14 @@ impl<'a> MyObject<'a> {
fn get(&self) -> Option<&'a u8> {
unsafe {
let obj = &*(self as *const _ as *const Object);
*obj.get_ivar("_number_ptr")
*obj.ivar("_number_ptr")
}
}

fn write(&mut self, number: u8) {
let ptr: &mut Option<&'a mut u8> = unsafe {
let obj = &mut *(self as *mut _ as *mut Object);
obj.get_mut_ivar("_number_ptr")
obj.ivar_mut("_number_ptr")
};
if let Some(ptr) = ptr {
**ptr = number;
4 changes: 2 additions & 2 deletions objc2-foundation/examples/custom_class.rs
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ impl MYObject {
fn number(&self) -> u32 {
unsafe {
let obj = &*(self as *const _ as *const Object);
*obj.get_ivar("_number")
*obj.ivar("_number")
}
}

@@ -62,7 +62,7 @@ unsafe impl INSObject for MYObject {
}

extern "C" fn my_object_get_number(this: &Object, _cmd: Sel) -> u32 {
unsafe { *this.get_ivar("_number") }
unsafe { *this.ivar("_number") }
}

unsafe {
5 changes: 5 additions & 0 deletions objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased - YYYY-MM-DD

### Changed
* **BREAKING**: Renamed:
- `Object::get_ivar` -> `Object::ivar`
- `Object::get_mut_ivar` -> `Object::ivar_mut`


## 0.3.0-alpha.5 - 2021-12-22

2 changes: 1 addition & 1 deletion objc2/README.md
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ decl.add_ivar::<u32>("_number");

// Add an ObjC method for getting the number
extern fn my_number_get(this: &Object, _cmd: Sel) -> u32 {
unsafe { *this.get_ivar("_number") }
unsafe { *this.ivar("_number") }
}
unsafe {
decl.add_method(sel!(number),
2 changes: 1 addition & 1 deletion objc2/examples/introspection.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ fn main() {
println!("NSObject address: {:p}", obj);

// Access an ivar of the object
let isa: *const Class = unsafe { *obj.get_ivar("isa") };
let isa: *const Class = unsafe { *obj.ivar("isa") };
println!("NSObject isa: {:?}", isa);

#[cfg(feature = "malloc")]
2 changes: 1 addition & 1 deletion objc2/src/declare.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ decl.add_ivar::<u32>("_number");
// Add an ObjC method for getting the number
extern fn my_number_get(this: &Object, _cmd: Sel) -> u32 {
unsafe { *this.get_ivar("_number") }
unsafe { *this.ivar("_number") }
}
unsafe {
decl.add_method(sel!(number),
22 changes: 14 additions & 8 deletions objc2/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -502,7 +502,7 @@ impl UnwindSafe for Protocol {}
impl RefUnwindSafe for Protocol {}
// Note that Unpin is not applicable.

fn get_ivar_offset<T: Encode>(cls: &Class, name: &str) -> isize {
fn ivar_offset<T: Encode>(cls: &Class, name: &str) -> isize {
match cls.instance_variable(name) {
Some(ivar) => {
assert!(T::ENCODING.equivalent_to_str(ivar.type_encoding()));
@@ -517,7 +517,7 @@ impl Object {
self as *const Self as *const _
}

/// Returns the class of this object.
/// Dynamically find the class of this object.
pub fn class(&self) -> &Class {
unsafe { &*(ffi::object_getClass(self.as_ptr()) as *const Class) }
}
@@ -532,8 +532,10 @@ impl Object {
/// # Safety
///
/// The caller must ensure that the ivar is actually of type `T`.
pub unsafe fn get_ivar<T: Encode>(&self, name: &str) -> &T {
let offset = get_ivar_offset::<T>(self.class(), name);
///
/// Library implementors should expose a safe interface to the ivar.
pub unsafe fn ivar<T: Encode>(&self, name: &str) -> &T {
let offset = ivar_offset::<T>(self.class(), name);
// `offset` is given in bytes, so we convert to `u8`
let ptr = self as *const Self as *const u8;
let ptr = unsafe { ptr.offset(offset) } as *const T;
@@ -550,8 +552,10 @@ impl Object {
/// # Safety
///
/// The caller must ensure that the ivar is actually of type `T`.
pub unsafe fn get_mut_ivar<T: Encode>(&mut self, name: &str) -> &mut T {
let offset = get_ivar_offset::<T>(self.class(), name);
///
/// Library implementors should expose a safe interface to the ivar.
pub unsafe fn ivar_mut<T: Encode>(&mut self, name: &str) -> &mut T {
let offset = ivar_offset::<T>(self.class(), name);
// `offset` is given in bytes, so we convert to `u8`
let ptr = self as *mut Self as *mut u8;
let ptr = unsafe { ptr.offset(offset) } as *mut T;
@@ -568,9 +572,11 @@ impl Object {
/// # Safety
///
/// The caller must ensure that the ivar is actually of type `T`.
///
/// Library implementors should expose a safe interface to the ivar.
pub unsafe fn set_ivar<T: Encode>(&mut self, name: &str, value: T) {
// SAFETY: Invariants upheld by caller
unsafe { *self.get_mut_ivar::<T>(name) = value };
unsafe { *self.ivar_mut::<T>(name) = value };
}

// objc_setAssociatedObject
@@ -713,7 +719,7 @@ mod tests {
assert_eq!(obj.class(), test_utils::custom_class());
let result: u32 = unsafe {
obj.set_ivar("_foo", 4u32);
*obj.get_ivar("_foo")
*obj.ivar("_foo")
};
assert_eq!(result, 4);
}
2 changes: 1 addition & 1 deletion objc2/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ pub(crate) fn custom_class() -> &'static Class {
}

extern "C" fn custom_obj_get_foo(this: &Object, _cmd: Sel) -> u32 {
unsafe { *this.get_ivar::<u32>("_foo") }
unsafe { *this.ivar::<u32>("_foo") }
}

extern "C" fn custom_obj_get_struct(_this: &Object, _cmd: Sel) -> CustomStruct {