Skip to content

Commit

Permalink
Use unstable-static-encoding-str feature in objc2 and objc2-foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed May 22, 2022
1 parent 22d14a5 commit 54e0d6d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions objc2-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ default = ["block"]
# Provided as a way to cut down on dependencies
block = ["block2"]

# Requires the `generic_const_exprs` feature
unstable-static-encoding-str = ["objc2-encode/unstable-static-encoding-str", "objc2/unstable-static-encoding-str"]

[dependencies]
block2 = { path = "../block2", version = "=0.2.0-alpha.3", optional = true }
objc2 = { path = "../objc2", version = "=0.3.0-alpha.6" }
objc-sys = { path = "../objc-sys", version = "=0.2.0-alpha.1" }
objc2-encode = { path = "../objc2-encode", optional = true }

[package.metadata.docs.rs]
default-target = "x86_64-apple-darwin"
11 changes: 5 additions & 6 deletions objc2-foundation/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ impl<T: 'static + Copy + Encode> NSValue<T> {
pub fn new(value: T) -> Id<Self, Shared> {
let cls = Self::class();
let bytes = &value as *const T as *const c_void;
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
#[cfg(not(feature = "unstable-static-encoding-str"))]
let encoding_ptr = CString::new(T::ENCODING.to_string()).unwrap().as_ptr();
#[cfg(feature = "unstable-static-encoding-str")]
let encoding_ptr = <objc2::encode::EncodingHelper<T>>::ENCODING_CSTR.cast::<c_char>();
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![
obj,
initWithBytes: bytes,
objCType: encoding.as_ptr(),
];
let obj: *mut Self = msg_send![obj, initWithBytes: bytes, objCType: encoding_ptr,];
Id::new(obj).unwrap()
}
}
Expand Down
3 changes: 3 additions & 0 deletions objc2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ malloc = ["malloc_buf"]
# Uses nightly features to make AutoreleasePool zero-cost even in debug mode
unstable_autoreleasesafe = []

# Requires the `generic_const_exprs` feature
unstable-static-encoding-str = ["objc2-encode/unstable-static-encoding-str"]

[dependencies]
malloc_buf = { version = "1.0", optional = true }
objc-sys = { path = "../objc-sys", version = "=0.2.0-alpha.1" }
Expand Down
14 changes: 6 additions & 8 deletions objc2/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,15 @@ impl ClassDecl {
/// If the ivar wasn't successfully added.
pub fn add_ivar<T: Encode>(&mut self, name: &str) {
let c_name = CString::new(name).unwrap();
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
#[cfg(not(feature = "unstable-static-encoding-str"))]
let encoding = CString::new(T::ENCODING.to_string()).unwrap().as_ptr();
#[cfg(feature = "unstable-static-encoding-str")]
let encoding_ptr =
<objc2_encode::EncodingHelper<T>>::ENCODING_CSTR.cast::<std::os::raw::c_char>();
let size = mem::size_of::<T>();
let align = log2_align_of::<T>();
let success = Bool::from_raw(unsafe {
ffi::class_addIvar(
self.as_ptr(),
c_name.as_ptr(),
size,
align,
encoding.as_ptr(),
)
ffi::class_addIvar(self.as_ptr(), c_name.as_ptr(), size, align, encoding_ptr)
});
assert!(success.as_bool(), "Failed to add ivar {}", name);
}
Expand Down

0 comments on commit 54e0d6d

Please sign in to comment.