Skip to content

Commit 9c31a41

Browse files
committed
Use static encoding strings
1 parent 050b502 commit 9c31a41

2 files changed

Lines changed: 7 additions & 15 deletions

File tree

crates/icrate/src/additions/Foundation/value.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#![cfg(feature = "Foundation_NSValue")]
2-
use alloc::string::ToString;
32
use core::fmt;
43
use core::hash;
54
use core::mem::MaybeUninit;
65
use core::str;
7-
use std::ffi::{CStr, CString};
6+
use std::ffi::CStr;
87

98
use objc2::encode::Encode;
109

@@ -35,7 +34,7 @@ impl NSValue {
3534
/// [`NSPoint`]: crate::Foundation::NSPoint
3635
pub fn new<T: 'static + Copy + Encode>(value: T) -> Id<Self> {
3736
let bytes: NonNull<T> = NonNull::from(&value);
38-
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
37+
let encoding = T::ENCODING_CSTR;
3938
unsafe {
4039
Self::initWithBytes_objCType(
4140
Self::alloc(),

crates/objc2/src/declare/mod.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mod ivar_encode;
124124
mod ivar_forwarding_impls;
125125

126126
use alloc::format;
127-
use alloc::string::ToString;
127+
use core::ffi::CStr;
128128
use core::mem;
129129
use core::mem::ManuallyDrop;
130130
use core::ptr;
@@ -550,17 +550,11 @@ impl ClassBuilder {
550550
/// happens if there already was an ivar with that name.
551551
pub fn add_ivar<T: Encode>(&mut self, name: &str) {
552552
// SAFETY: The encoding is correct
553-
unsafe { self.add_ivar_inner::<T>(name, &T::ENCODING) }
553+
unsafe { self.add_ivar_inner::<T>(name, &T::ENCODING_CSTR) }
554554
}
555555

556556
// Monomorphized version
557-
unsafe fn add_ivar_inner_mono(
558-
&mut self,
559-
name: &str,
560-
size: usize,
561-
align: u8,
562-
encoding: &Encoding,
563-
) {
557+
unsafe fn add_ivar_inner_mono(&mut self, name: &str, size: usize, align: u8, encoding: &CStr) {
564558
// `class_addIvar` sadly doesn't check this for us.
565559
//
566560
// We must _always_ do the check, since there is no way for the user
@@ -575,7 +569,6 @@ impl ClassBuilder {
575569
}
576570

577571
let c_name = CString::new(name).unwrap();
578-
let encoding = CString::new(encoding.to_string()).unwrap();
579572
let success = Bool::from_raw(unsafe {
580573
ffi::class_addIvar(
581574
self.as_mut_ptr(),
@@ -588,7 +581,7 @@ impl ClassBuilder {
588581
assert!(success.as_bool(), "failed to add ivar {name}");
589582
}
590583

591-
unsafe fn add_ivar_inner<T>(&mut self, name: &str, encoding: &Encoding) {
584+
unsafe fn add_ivar_inner<T>(&mut self, name: &str, encoding: &CStr) {
592585
unsafe { self.add_ivar_inner_mono(name, mem::size_of::<T>(), T::LOG2_ALIGNMENT, encoding) }
593586
}
594587

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

606599
/// Adds the given protocol to self.

0 commit comments

Comments
 (0)