Skip to content

Commit 5acef38

Browse files
committed
Use unstable-static-encoding-str feature in objc2 and objc2-foundation
1 parent b354d5e commit 5acef38

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

objc2-foundation/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ default = ["block"]
2323
# Provided as a way to cut down on dependencies
2424
block = ["block2"]
2525

26+
# Requires the `generic_const_exprs` feature
27+
unstable-static-encoding-str = ["objc2-encode/unstable-static-encoding-str", "objc2/unstable-static-encoding-str"]
28+
2629
[dependencies]
2730
block2 = { path = "../block2", version = "=0.2.0-alpha.3", optional = true }
2831
objc2 = { path = "../objc2", version = "=0.3.0-alpha.6" }
2932
objc-sys = { path = "../objc-sys", version = "=0.2.0-alpha.1" }
33+
objc2-encode = { path = "../objc2-encode", optional = true }
3034

3135
[package.metadata.docs.rs]
3236
default-target = "x86_64-apple-darwin"

objc2-foundation/src/value.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ impl<T: 'static + Copy + Encode> NSValue<T> {
6565
pub fn new(value: T) -> Id<Self, Shared> {
6666
let cls = Self::class();
6767
let bytes = &value as *const T as *const c_void;
68-
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
68+
#[cfg(not(feature = "unstable-static-encoding-str"))]
69+
let encoding_ptr = CString::new(T::ENCODING.to_string()).unwrap().as_ptr();
70+
#[cfg(feature = "unstable-static-encoding-str")]
71+
let encoding_ptr = <objc2::encode::EncodingHelper<T>>::ENCODING_CSTR.cast::<c_char>();
6972
unsafe {
7073
let obj: *mut Self = msg_send![cls, alloc];
71-
let obj: *mut Self = msg_send![
72-
obj,
73-
initWithBytes: bytes,
74-
objCType: encoding.as_ptr(),
75-
];
74+
let obj: *mut Self = msg_send![obj, initWithBytes: bytes, objCType: encoding_ptr,];
7675
Id::new(obj).unwrap()
7776
}
7877
}

objc2/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ malloc = ["malloc_buf"]
4040
# Uses nightly features to make AutoreleasePool zero-cost even in debug mode
4141
unstable_autoreleasesafe = []
4242

43+
# Requires the `generic_const_exprs` feature
44+
unstable-static-encoding-str = ["objc2-encode/unstable-static-encoding-str"]
45+
4346
[dependencies]
4447
malloc_buf = { version = "1.0", optional = true }
4548
objc-sys = { path = "../objc-sys", version = "=0.2.0-alpha.1" }

objc2/src/declare.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,15 @@ impl ClassDecl {
262262
/// If the ivar wasn't successfully added.
263263
pub fn add_ivar<T: Encode>(&mut self, name: &str) {
264264
let c_name = CString::new(name).unwrap();
265-
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
265+
#[cfg(not(feature = "unstable-static-encoding-str"))]
266+
let encoding = CString::new(T::ENCODING.to_string()).unwrap().as_ptr();
267+
#[cfg(feature = "unstable-static-encoding-str")]
268+
let encoding_ptr =
269+
<objc2_encode::EncodingHelper<T>>::ENCODING_CSTR.cast::<std::os::raw::c_char>();
266270
let size = mem::size_of::<T>();
267271
let align = log2_align_of::<T>();
268272
let success = Bool::from_raw(unsafe {
269-
ffi::class_addIvar(
270-
self.as_ptr(),
271-
c_name.as_ptr(),
272-
size,
273-
align,
274-
encoding.as_ptr(),
275-
)
273+
ffi::class_addIvar(self.as_ptr(), c_name.as_ptr(), size, align, encoding_ptr)
276274
});
277275
assert!(success.as_bool(), "Failed to add ivar {}", name);
278276
}

0 commit comments

Comments
 (0)