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 Nov 3, 2021
1 parent 19e9e4e commit 94ae38d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions objc2-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ 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", optional = true }
objc2 = { path = "../objc2" }
objc-sys = { path = "../objc-sys" }
objc2-encode = { path = "../objc2-encode", optional = true }
12 changes: 7 additions & 5 deletions objc2-foundation/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ pub unsafe trait INSValue: INSObject {
fn new(value: Self::Value) -> Id<Self, Self::Ownership> {
let cls = Self::class();
let bytes = &value as *const Self::Value as *const c_void;
#[cfg(not(feature = "unstable_static_encoding_str"))]
let encoding = CString::new(Self::Value::ENCODING.to_string()).unwrap();
#[cfg(not(feature = "unstable_static_encoding_str"))]
let encoding_ptr = encoding.as_ptr();
#[cfg(feature = "unstable_static_encoding_str")]
let encoding_ptr =
<objc2_encode::EncodingHelper<Self::Value::ENCODING>>::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(NonNull::new_unchecked(obj))
}
}
Expand Down
3 changes: 3 additions & 0 deletions objc2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ catch_all = ["exception"]
verify_message = []
unstable_autoreleasesafe = []

# Requires the `generic_const_exprs` feature
unstable_static_encoding_str = ["objc2-encode/unstable_static_encoding_str"]

[dependencies]
malloc_buf = "1.0"
objc-sys = { path = "../objc-sys" }
Expand Down
14 changes: 7 additions & 7 deletions objc2/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,17 @@ 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();
#[cfg(not(feature = "unstable_static_encoding_str"))]
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
#[cfg(not(feature = "unstable_static_encoding_str"))]
let encoding_ptr = encoding.as_ptr();
#[cfg(feature = "unstable_static_encoding_str")]
let encoding_ptr = <objc2_encode::EncodingHelper<T::ENCODING>>::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 {
runtime::class_addIvar(
self.cls as _,
c_name.as_ptr(),
size,
align,
encoding.as_ptr(),
)
runtime::class_addIvar(self.cls as _, c_name.as_ptr(), size, align, encoding_ptr)
});
assert!(success.is_true(), "Failed to add ivar {}", name);
}
Expand Down

0 comments on commit 94ae38d

Please sign in to comment.