Skip to content

Commit

Permalink
Add a few NSData methods
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jul 5, 2022
1 parent 7f79d1c commit 819d96d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
3 changes: 1 addition & 2 deletions objc2-foundation-sys/src/generated_nsstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ use objc2::rc::{Allocated, Id, Unknown};
use objc2::runtime::{Bool, Object};
use objc2::{class, msg_send, msg_send_id, Encoding, Message, RefEncode};

use crate::NSObject;
use crate::{NSData, NSObject};

pub type NSRange = [NSUInteger; 2];
pub type NSComparisonResult = NSInteger;

pub type NSCoder = NSObject;
pub type NSLocale = NSObject;
pub type NSError = NSObject;
pub type NSData = NSObject;
pub type NSDictionary = NSObject;
pub type NSCharacterSet = NSObject;
pub type NSURL = NSObject;
Expand Down
58 changes: 58 additions & 0 deletions objc2-foundation-sys/src/generated_others_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use core::ptr::NonNull;

use std::os::raw::c_void;

use objc2::ffi::{NSInteger, NSUInteger};
use objc2::rc::{Allocated, Id, Unknown};
use objc2::runtime::Object;
Expand Down Expand Up @@ -71,3 +73,59 @@ impl NSArray {
msg_send![self, getObjects: objects, range: range]
}
}

#[repr(transparent)]
pub struct NSData(NSObject);
impl core::ops::Deref for NSData {
type Target = NSObject;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl core::ops::DerefMut for NSData {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
unsafe impl Message for NSData {}
unsafe impl RefEncode for NSData {
const ENCODING_REF: Encoding<'static> = Encoding::Object;
}
impl NSData {
pub unsafe fn alloc() -> Option<Id<Allocated<Self>, Unknown>> {
msg_send_id![class!(NSData), alloc]
}
}
impl NSData {
pub unsafe fn length(&self) -> NSUInteger {
msg_send![self, length]
}
pub unsafe fn bytes(&self) -> *const c_void {
msg_send![self, bytes]
}
}
impl NSData {
pub unsafe fn initWithBytes_length_(
this: Option<Id<Allocated<Self>, Unknown>>,
bytes: *const c_void,
length: NSUInteger,
) -> Id<Self, Unknown> {
msg_send_id![this, initWithBytes: bytes, length: length].unwrap_unchecked()
}
pub unsafe fn initWithBytesNoCopy_length_deallocator_(
this: Option<Id<Allocated<Self>, Unknown>>,
bytes: *mut c_void,
length: NSUInteger,
deallocator: *mut c_void,
) -> Id<Self, Unknown> {
msg_send_id![
this,
initWithBytesNoCopy: bytes,
length: length,
deallocator: deallocator,
]
.unwrap_unchecked()
}
}
10 changes: 7 additions & 3 deletions objc2-foundation/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use objc2::rc::{DefaultId, Id, Owned, Shared};
use objc2::runtime::{Class, Object};
use objc2::{msg_send, msg_send_id};

use super::{NSCopying, NSMutableCopying, NSObject, NSRange};
use super::{ffi, NSCopying, NSMutableCopying, NSObject, NSRange};

object! {
/// A static byte buffer in memory.
Expand Down Expand Up @@ -42,17 +42,21 @@ unsafe impl Send for NSMutableData {}
impl NSData {
unsafe_def_fn!(fn new -> Shared);

fn r(&self) -> &ffi::NSData {
unsafe { &*(self as *const Self as *const ffi::NSData) }
}

#[doc(alias = "length")]
pub fn len(&self) -> usize {
unsafe { msg_send![self, length] }
unsafe { self.r().length() }
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn bytes(&self) -> &[u8] {
let ptr: *const c_void = unsafe { msg_send![self, bytes] };
let ptr: *const c_void = unsafe { self.r().bytes() };
let ptr: *const u8 = ptr.cast();
// The bytes pointer may be null for length zero
if ptr.is_null() {
Expand Down

0 comments on commit 819d96d

Please sign in to comment.