Skip to content

Commit

Permalink
Use static encoding strings
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 1, 2023
1 parent 050b502 commit 9c31a41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
5 changes: 2 additions & 3 deletions crates/icrate/src/additions/Foundation/value.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -35,7 +34,7 @@ impl NSValue {
/// [`NSPoint`]: crate::Foundation::NSPoint
pub fn new<T: 'static + Copy + Encode>(value: T) -> Id<Self> {
let bytes: NonNull<T> = NonNull::from(&value);
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
let encoding = T::ENCODING_CSTR;
unsafe {
Self::initWithBytes_objCType(
Self::alloc(),
Expand Down
17 changes: 5 additions & 12 deletions crates/objc2/src/declare/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -550,17 +550,11 @@ impl ClassBuilder {
/// happens if there already was an ivar with that name.
pub fn add_ivar<T: Encode>(&mut self, name: &str) {
// SAFETY: The encoding is correct
unsafe { self.add_ivar_inner::<T>(name, &T::ENCODING) }
unsafe { self.add_ivar_inner::<T>(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
Expand All @@ -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(),
Expand All @@ -588,7 +581,7 @@ impl ClassBuilder {
assert!(success.as_bool(), "failed to add ivar {name}");
}

unsafe fn add_ivar_inner<T>(&mut self, name: &str, encoding: &Encoding) {
unsafe fn add_ivar_inner<T>(&mut self, name: &str, encoding: &CStr) {
unsafe { self.add_ivar_inner_mono(name, mem::size_of::<T>(), T::LOG2_ALIGNMENT, encoding) }
}

Expand All @@ -600,7 +593,7 @@ impl ClassBuilder {
/// Same as [`ClassBuilder::add_ivar`].
pub fn add_static_ivar<T: IvarType>(&mut self) {
// SAFETY: The encoding is correct
unsafe { self.add_ivar_inner::<T::Type>(T::NAME, &T::Type::ENCODING) }
unsafe { self.add_ivar_inner::<T::Type>(T::NAME, &T::Type::ENCODING_CSTR) }
}

/// Adds the given protocol to self.
Expand Down

0 comments on commit 9c31a41

Please sign in to comment.