Skip to content

Commit 94ae38d

Browse files
committed
Use unstable_static_encoding_str feature in objc2 and objc2-foundation
1 parent 19e9e4e commit 94ae38d

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

objc2-foundation/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ 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", optional = true }
2831
objc2 = { path = "../objc2" }
2932
objc-sys = { path = "../objc-sys" }
33+
objc2-encode = { path = "../objc2-encode", optional = true }

objc2-foundation/src/value.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ pub unsafe trait INSValue: INSObject {
5757
fn new(value: Self::Value) -> Id<Self, Self::Ownership> {
5858
let cls = Self::class();
5959
let bytes = &value as *const Self::Value as *const c_void;
60+
#[cfg(not(feature = "unstable_static_encoding_str"))]
6061
let encoding = CString::new(Self::Value::ENCODING.to_string()).unwrap();
62+
#[cfg(not(feature = "unstable_static_encoding_str"))]
63+
let encoding_ptr = encoding.as_ptr();
64+
#[cfg(feature = "unstable_static_encoding_str")]
65+
let encoding_ptr =
66+
<objc2_encode::EncodingHelper<Self::Value::ENCODING>>::ENCODING_CSTR.cast::<c_char>();
6167
unsafe {
6268
let obj: *mut Self = msg_send![cls, alloc];
63-
let obj: *mut Self = msg_send![
64-
obj,
65-
initWithBytes: bytes,
66-
objCType: encoding.as_ptr(),
67-
];
69+
let obj: *mut Self = msg_send![obj, initWithBytes: bytes, objCType: encoding_ptr];
6870
Id::new(NonNull::new_unchecked(obj))
6971
}
7072
}

objc2/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ catch_all = ["exception"]
3232
verify_message = []
3333
unstable_autoreleasesafe = []
3434

35+
# Requires the `generic_const_exprs` feature
36+
unstable_static_encoding_str = ["objc2-encode/unstable_static_encoding_str"]
37+
3538
[dependencies]
3639
malloc_buf = "1.0"
3740
objc-sys = { path = "../objc-sys" }

objc2/src/declare.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,17 @@ impl ClassDecl {
243243
/// If the ivar wasn't successfully added.
244244
pub fn add_ivar<T: Encode>(&mut self, name: &str) {
245245
let c_name = CString::new(name).unwrap();
246+
#[cfg(not(feature = "unstable_static_encoding_str"))]
246247
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
248+
#[cfg(not(feature = "unstable_static_encoding_str"))]
249+
let encoding_ptr = encoding.as_ptr();
250+
#[cfg(feature = "unstable_static_encoding_str")]
251+
let encoding_ptr = <objc2_encode::EncodingHelper<T::ENCODING>>::ENCODING_CSTR
252+
.cast::<std::os::raw::c_char>();
247253
let size = mem::size_of::<T>();
248254
let align = log2_align_of::<T>();
249255
let success = Bool::from_raw(unsafe {
250-
runtime::class_addIvar(
251-
self.cls as _,
252-
c_name.as_ptr(),
253-
size,
254-
align,
255-
encoding.as_ptr(),
256-
)
256+
runtime::class_addIvar(self.cls as _, c_name.as_ptr(), size, align, encoding_ptr)
257257
});
258258
assert!(success.is_true(), "Failed to add ivar {}", name);
259259
}

0 commit comments

Comments
 (0)