From 54e0d6dedc5e02288c96bcd51838a49a166f018c Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 22 May 2022 17:46:22 +0200 Subject: [PATCH] Use `unstable-static-encoding-str` feature in objc2 and objc2-foundation --- objc2-foundation/Cargo.toml | 4 ++++ objc2-foundation/src/value.rs | 11 +++++------ objc2/Cargo.toml | 3 +++ objc2/src/declare.rs | 14 ++++++-------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/objc2-foundation/Cargo.toml b/objc2-foundation/Cargo.toml index 097125195..35fbc3291 100644 --- a/objc2-foundation/Cargo.toml +++ b/objc2-foundation/Cargo.toml @@ -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" diff --git a/objc2-foundation/src/value.rs b/objc2-foundation/src/value.rs index a79a3f30c..83058cc61 100644 --- a/objc2-foundation/src/value.rs +++ b/objc2-foundation/src/value.rs @@ -65,14 +65,13 @@ impl NSValue { pub fn new(value: T) -> Id { 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 = >::ENCODING_CSTR.cast::(); 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() } } diff --git a/objc2/Cargo.toml b/objc2/Cargo.toml index 083ed586a..247b0b49d 100644 --- a/objc2/Cargo.toml +++ b/objc2/Cargo.toml @@ -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" } diff --git a/objc2/src/declare.rs b/objc2/src/declare.rs index 2c2da05d9..5e774f099 100644 --- a/objc2/src/declare.rs +++ b/objc2/src/declare.rs @@ -262,17 +262,15 @@ impl ClassDecl { /// If the ivar wasn't successfully added. pub fn add_ivar(&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 = + >::ENCODING_CSTR.cast::(); let size = mem::size_of::(); let align = log2_align_of::(); 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); }