Skip to content

Commit 0b42ca9

Browse files
committed
Improve NSMutableData
1 parent 063d612 commit 0b42ca9

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

objc2_foundation/src/data.rs

+48-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(feature = "block")]
22
use alloc::vec::Vec;
3-
use core::ops::Range;
3+
use core::ops::{Deref, DerefMut, Range};
44
use core::slice;
55
use core::{ffi::c_void, ptr::NonNull};
66

@@ -98,45 +98,45 @@ unsafe impl INSMutableCopying for NSData {
9898
type Output = NSMutableData;
9999
}
100100

101+
impl Deref for NSData {
102+
type Target = [u8];
103+
104+
fn deref(&self) -> &[u8] {
105+
self.bytes()
106+
}
107+
}
108+
101109
pub unsafe trait INSMutableData: INSData {
102110
fn bytes_mut(&mut self) -> &mut [u8] {
103111
let ptr: *mut c_void = unsafe { msg_send![self, mutableBytes] };
104112
// The bytes pointer may be null for length zero
105-
let (ptr, len) = if ptr.is_null() {
106-
(0x1 as *mut u8, 0)
113+
if ptr.is_null() {
114+
&mut []
107115
} else {
108-
(ptr as *mut u8, self.len())
109-
};
110-
unsafe { slice::from_raw_parts_mut(ptr, len) }
116+
unsafe { slice::from_raw_parts_mut(ptr as *mut u8, self.len()) }
117+
}
111118
}
112119

120+
/// Expands with zeroes, or truncates the buffer.
113121
fn set_len(&mut self, len: usize) {
114-
unsafe {
115-
let _: () = msg_send![self, setLength: len];
116-
}
122+
unsafe { msg_send![self, setLength: len] }
117123
}
118124

119125
fn append(&mut self, bytes: &[u8]) {
120126
let bytes_ptr = bytes.as_ptr() as *const c_void;
121-
unsafe {
122-
let _: () = msg_send![
123-
self,
124-
appendBytes: bytes_ptr,
125-
length:bytes.len(),
126-
];
127-
}
127+
unsafe { msg_send![self, appendBytes: bytes_ptr, length: bytes.len()] }
128128
}
129129

130130
fn replace_range(&mut self, range: Range<usize>, bytes: &[u8]) {
131131
let range = NSRange::from(range);
132132
let bytes_ptr = bytes.as_ptr() as *const c_void;
133133
unsafe {
134-
let _: () = msg_send![
134+
msg_send![
135135
self,
136-
replaceBytesInRange:range,
137-
withBytes:bytes_ptr,
138-
length:bytes.len(),
139-
];
136+
replaceBytesInRange: range,
137+
withBytes: bytes_ptr,
138+
length: bytes.len(),
139+
]
140140
}
141141
}
142142

@@ -160,6 +160,20 @@ unsafe impl INSMutableCopying for NSMutableData {
160160
type Output = NSMutableData;
161161
}
162162

163+
impl Deref for NSMutableData {
164+
type Target = [u8];
165+
166+
fn deref(&self) -> &[u8] {
167+
self.bytes()
168+
}
169+
}
170+
171+
impl DerefMut for NSMutableData {
172+
fn deref_mut(&mut self) -> &mut [u8] {
173+
self.bytes_mut()
174+
}
175+
}
176+
163177
#[cfg(test)]
164178
mod tests {
165179
use super::{INSData, INSMutableData, NSData, NSMutableData};
@@ -171,8 +185,8 @@ mod tests {
171185
fn test_bytes() {
172186
let bytes = [3, 7, 16, 52, 112, 19];
173187
let data = NSData::with_bytes(&bytes);
174-
assert!(data.len() == bytes.len());
175-
assert!(data.bytes() == bytes);
188+
assert_eq!(data.len(), bytes.len());
189+
assert_eq!(data.bytes(), bytes);
176190
}
177191

178192
#[test]
@@ -185,43 +199,43 @@ mod tests {
185199
fn test_bytes_mut() {
186200
let mut data = NSMutableData::with_bytes(&[7, 16]);
187201
data.bytes_mut()[0] = 3;
188-
assert!(data.bytes() == [3, 16]);
202+
assert_eq!(data.bytes(), [3, 16]);
189203
}
190204

191205
#[test]
192206
fn test_set_len() {
193207
let mut data = NSMutableData::with_bytes(&[7, 16]);
194208
data.set_len(4);
195-
assert!(data.len() == 4);
196-
assert!(data.bytes() == [7, 16, 0, 0]);
209+
assert_eq!(data.len(), 4);
210+
assert_eq!(data.bytes(), [7, 16, 0, 0]);
197211

198212
data.set_len(1);
199-
assert!(data.len() == 1);
200-
assert!(data.bytes() == [7]);
213+
assert_eq!(data.len(), 1);
214+
assert_eq!(data.bytes(), [7]);
201215
}
202216

203217
#[test]
204218
fn test_append() {
205219
let mut data = NSMutableData::with_bytes(&[7, 16]);
206220
data.append(&[3, 52]);
207-
assert!(data.len() == 4);
208-
assert!(data.bytes() == [7, 16, 3, 52]);
221+
assert_eq!(data.len(), 4);
222+
assert_eq!(data.bytes(), [7, 16, 3, 52]);
209223
}
210224

211225
#[test]
212226
fn test_replace() {
213227
let mut data = NSMutableData::with_bytes(&[7, 16]);
214228
data.replace_range(0..0, &[3]);
215-
assert!(data.bytes() == [3, 7, 16]);
229+
assert_eq!(data.bytes(), [3, 7, 16]);
216230

217231
data.replace_range(1..2, &[52, 13]);
218-
assert!(data.bytes() == [3, 52, 13, 16]);
232+
assert_eq!(data.bytes(), [3, 52, 13, 16]);
219233

220234
data.replace_range(2..4, &[6]);
221-
assert!(data.bytes() == [3, 52, 6]);
235+
assert_eq!(data.bytes(), [3, 52, 6]);
222236

223237
data.set_bytes(&[8, 17]);
224-
assert!(data.bytes() == [8, 17]);
238+
assert_eq!(data.bytes(), [8, 17]);
225239
}
226240

227241
#[cfg(feature = "block")]

0 commit comments

Comments
 (0)