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 Jan 12, 2022
1 parent 0007dbc commit e26f51f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 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 @@ -10,15 +10,14 @@ use objc2::rc::{Id, Unknown};
use objc2::runtime::{Bool, Object};
use objc2::{class, msg_send, Encoding, Message, RefEncode};

use crate::{Autoreleased, NSArray, NSObject};
use crate::{Autoreleased, NSArray, 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
67 changes: 65 additions & 2 deletions objc2-foundation-sys/src/generated_others_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use core::mem::ManuallyDrop;
use core::ptr::NonNull;

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

use objc2::ffi::{NSInteger, NSUInteger};
use objc2::rc::{Id, Unknown};
use objc2::runtime::Object;
Expand Down Expand Up @@ -55,8 +57,12 @@ impl NSArray {
let this = ManuallyDrop::new(this);
Id::new(msg_send![this, initWithObjects: objects, count: cnt])
}
pub unsafe fn initWithCoder_(&self, coder: NonNull<NSCoder>) -> Option<Id<Self, Unknown>> {
Id::new_null(msg_send![self, initWithCoder: coder])
pub unsafe fn initWithCoder_(
this: Id<Self, Unknown>,
coder: NonNull<NSCoder>,
) -> Option<Id<Self, Unknown>> {
let this = ManuallyDrop::new(this);
Id::new_null(msg_send![this, initWithCoder: coder])
}
pub unsafe fn count(&self) -> NSUInteger {
msg_send![self, count]
Expand All @@ -71,3 +77,60 @@ 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<Self, Unknown>> {
Id::new_null(msg_send![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: Id<Self, Unknown>,
bytes: *const c_void,
length: NSUInteger,
) -> Id<Self, Unknown> {
let this = ManuallyDrop::new(this);
Id::new(msg_send![this, initWithBytes: bytes, length: length])
}
pub unsafe fn initWithBytesNoCopy_length_deallocator_(
this: Id<Self, Unknown>,
bytes: *mut c_void,
length: NSUInteger,
deallocator: *mut c_void,
) -> Id<Self, Unknown> {
let this = ManuallyDrop::new(this);
Id::new(msg_send![
this,
initWithBytesNoCopy: bytes,
length: length,
deallocator: deallocator,
])
}
}
12 changes: 8 additions & 4 deletions objc2-foundation/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use objc2::msg_send;
use objc2::rc::{DefaultId, Id, Owned, Shared};
use objc2::runtime::{Class, Object};

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

object! {
unsafe pub struct NSData: NSObject;
Expand All @@ -29,21 +29,25 @@ 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) }
}

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 = unsafe { self.r().bytes() } as *const u8;
// The bytes pointer may be null for length zero
if ptr.is_null() {
&[]
} else {
unsafe { slice::from_raw_parts(ptr as *const u8, self.len()) }
unsafe { slice::from_raw_parts(ptr, self.len()) }
}
}

Expand Down

0 comments on commit e26f51f

Please sign in to comment.