From 9c31a419f9f0140f7ae2694d4924cdf76674930a Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 1 Sep 2023 19:39:01 +0200 Subject: [PATCH] Use static encoding strings --- crates/icrate/src/additions/Foundation/value.rs | 5 ++--- crates/objc2/src/declare/mod.rs | 17 +++++------------ 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/crates/icrate/src/additions/Foundation/value.rs b/crates/icrate/src/additions/Foundation/value.rs index 78a69358d..f2af7c0b4 100644 --- a/crates/icrate/src/additions/Foundation/value.rs +++ b/crates/icrate/src/additions/Foundation/value.rs @@ -1,10 +1,9 @@ #![cfg(feature = "Foundation_NSValue")] -use alloc::string::ToString; use core::fmt; use core::hash; use core::mem::MaybeUninit; use core::str; -use std::ffi::{CStr, CString}; +use std::ffi::CStr; use objc2::encode::Encode; @@ -35,7 +34,7 @@ impl NSValue { /// [`NSPoint`]: crate::Foundation::NSPoint pub fn new(value: T) -> Id { let bytes: NonNull = NonNull::from(&value); - let encoding = CString::new(T::ENCODING.to_string()).unwrap(); + let encoding = T::ENCODING_CSTR; unsafe { Self::initWithBytes_objCType( Self::alloc(), diff --git a/crates/objc2/src/declare/mod.rs b/crates/objc2/src/declare/mod.rs index 97088959f..dba966091 100644 --- a/crates/objc2/src/declare/mod.rs +++ b/crates/objc2/src/declare/mod.rs @@ -124,7 +124,7 @@ mod ivar_encode; mod ivar_forwarding_impls; use alloc::format; -use alloc::string::ToString; +use core::ffi::CStr; use core::mem; use core::mem::ManuallyDrop; use core::ptr; @@ -550,17 +550,11 @@ impl ClassBuilder { /// happens if there already was an ivar with that name. pub fn add_ivar(&mut self, name: &str) { // SAFETY: The encoding is correct - unsafe { self.add_ivar_inner::(name, &T::ENCODING) } + unsafe { self.add_ivar_inner::(name, &T::ENCODING_CSTR) } } // Monomorphized version - unsafe fn add_ivar_inner_mono( - &mut self, - name: &str, - size: usize, - align: u8, - encoding: &Encoding, - ) { + unsafe fn add_ivar_inner_mono(&mut self, name: &str, size: usize, align: u8, encoding: &CStr) { // `class_addIvar` sadly doesn't check this for us. // // We must _always_ do the check, since there is no way for the user @@ -575,7 +569,6 @@ impl ClassBuilder { } let c_name = CString::new(name).unwrap(); - let encoding = CString::new(encoding.to_string()).unwrap(); let success = Bool::from_raw(unsafe { ffi::class_addIvar( self.as_mut_ptr(), @@ -588,7 +581,7 @@ impl ClassBuilder { assert!(success.as_bool(), "failed to add ivar {name}"); } - unsafe fn add_ivar_inner(&mut self, name: &str, encoding: &Encoding) { + unsafe fn add_ivar_inner(&mut self, name: &str, encoding: &CStr) { unsafe { self.add_ivar_inner_mono(name, mem::size_of::(), T::LOG2_ALIGNMENT, encoding) } } @@ -600,7 +593,7 @@ impl ClassBuilder { /// Same as [`ClassBuilder::add_ivar`]. pub fn add_static_ivar(&mut self) { // SAFETY: The encoding is correct - unsafe { self.add_ivar_inner::(T::NAME, &T::Type::ENCODING) } + unsafe { self.add_ivar_inner::(T::NAME, &T::Type::ENCODING_CSTR) } } /// Adds the given protocol to self.