Skip to content

Commit 33f1daa

Browse files
committed
Update to the new malloc_buffer
1 parent b7dfa8f commit 33f1daa

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exception = ["objc_exception"]
2424
verify_message = []
2525

2626
[dependencies]
27-
malloc_buf = "0.0"
27+
malloc_buf = "1.0"
2828

2929
[dependencies.objc_exception]
3030
version = "0.1"

src/encode.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::ffi::CStr;
21
use std::fmt;
32
use std::os::raw::{c_char, c_void};
43
use std::str;
5-
use malloc_buf::MallocBuffer;
4+
5+
use malloc_buf::Malloc;
66

77
use runtime::{Class, Object, Sel};
88

@@ -26,7 +26,7 @@ enum Code {
2626
Slice(&'static str),
2727
Owned(String),
2828
Inline(u8, [u8; CODE_INLINE_CAP]),
29-
Malloc(MallocBuffer<u8>)
29+
Malloc(Malloc<str>)
3030
}
3131

3232
/// An Objective-C type encoding.
@@ -52,9 +52,7 @@ impl Encoding {
5252
Code::Inline(len, ref bytes) => unsafe {
5353
str::from_utf8_unchecked(&bytes[..len as usize])
5454
},
55-
Code::Malloc(ref buf) => unsafe {
56-
str::from_utf8_unchecked(&buf[..buf.len() - 1])
57-
},
55+
Code::Malloc(ref buf) => &*buf,
5856
}
5957
}
6058
}
@@ -101,11 +99,8 @@ pub fn from_str(code: &str) -> Encoding {
10199
}
102100

103101
pub unsafe fn from_malloc_str(ptr: *mut c_char) -> Encoding {
104-
let s = CStr::from_ptr(ptr);
105-
let bytes = s.to_bytes_with_nul();
106-
assert!(str::from_utf8(bytes).is_ok());
107-
let buf = MallocBuffer::new(ptr as *mut u8, bytes.len()).unwrap();
108-
Encoding { code: Code::Malloc(buf) }
102+
let buf = Malloc::from_c_str(ptr);
103+
Encoding { code: Code::Malloc(buf.unwrap()) }
109104
}
110105

111106
/// Types that have an Objective-C type encoding.

src/runtime.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fmt;
88
use std::os::raw::{c_char, c_int, c_uint, c_void};
99
use std::ptr;
1010
use std::str;
11-
use malloc_buf::MallocBuffer;
11+
use malloc_buf::Malloc;
1212

1313
use encode;
1414
use {Encode, Encoding};
@@ -284,11 +284,11 @@ impl Class {
284284
}
285285

286286
/// Obtains the list of registered class definitions.
287-
pub fn classes() -> MallocBuffer<&'static Class> {
287+
pub fn classes() -> Malloc<[&'static Class]> {
288288
unsafe {
289289
let mut count: c_uint = 0;
290290
let classes = objc_copyClassList(&mut count);
291-
MallocBuffer::new(classes as *mut _, count as usize).unwrap()
291+
Malloc::from_array(classes as *mut _, count as usize)
292292
}
293293
}
294294

@@ -351,11 +351,11 @@ impl Class {
351351
}
352352

353353
/// Describes the instance methods implemented by self.
354-
pub fn instance_methods(&self) -> MallocBuffer<&Method> {
354+
pub fn instance_methods(&self) -> Malloc<[&Method]> {
355355
unsafe {
356356
let mut count: c_uint = 0;
357357
let methods = class_copyMethodList(self, &mut count);
358-
MallocBuffer::new(methods as *mut _, count as usize).unwrap()
358+
Malloc::from_array(methods as *mut _, count as usize)
359359
}
360360

361361
}
@@ -366,20 +366,20 @@ impl Class {
366366
}
367367

368368
/// Get a list of the protocols to which this class conforms.
369-
pub fn adopted_protocols(&self) -> MallocBuffer<&Protocol> {
369+
pub fn adopted_protocols(&self) -> Malloc<[&Protocol]> {
370370
unsafe {
371371
let mut count: c_uint = 0;
372372
let protos = class_copyProtocolList(self, &mut count);
373-
MallocBuffer::new(protos as *mut _, count as usize).unwrap()
373+
Malloc::from_array(protos as *mut _, count as usize)
374374
}
375375
}
376376

377377
/// Describes the instance variables declared by self.
378-
pub fn instance_variables(&self) -> MallocBuffer<&Ivar> {
378+
pub fn instance_variables(&self) -> Malloc<[&Ivar]> {
379379
unsafe {
380380
let mut count: c_uint = 0;
381381
let ivars = class_copyIvarList(self, &mut count);
382-
MallocBuffer::new(ivars as *mut _, count as usize).unwrap()
382+
Malloc::from_array(ivars as *mut _, count as usize)
383383
}
384384
}
385385
}
@@ -412,20 +412,20 @@ impl Protocol {
412412
}
413413

414414
/// Obtains the list of registered protocol definitions.
415-
pub fn protocols() -> MallocBuffer<&'static Protocol> {
415+
pub fn protocols() -> Malloc<[&'static Protocol]> {
416416
unsafe {
417417
let mut count: c_uint = 0;
418418
let protocols = objc_copyProtocolList(&mut count);
419-
MallocBuffer::new(protocols as *mut _, count as usize).unwrap()
419+
Malloc::from_array(protocols as *mut _, count as usize)
420420
}
421421
}
422422

423423
/// Get a list of the protocols to which this protocol conforms.
424-
pub fn adopted_protocols(&self) -> MallocBuffer<&Protocol> {
424+
pub fn adopted_protocols(&self) -> Malloc<[&Protocol]> {
425425
unsafe {
426426
let mut count: c_uint = 0;
427427
let protocols = protocol_copyProtocolList(self, &mut count);
428-
MallocBuffer::new(protocols as *mut _, count as usize).unwrap()
428+
Malloc::from_array(protocols as *mut _, count as usize)
429429
}
430430
}
431431

0 commit comments

Comments
 (0)