diff --git a/objc2-foundation-sys/src/generated_nsstring.rs b/objc2-foundation-sys/src/generated_nsstring.rs index 2df6e5654..c6eaa6255 100644 --- a/objc2-foundation-sys/src/generated_nsstring.rs +++ b/objc2-foundation-sys/src/generated_nsstring.rs @@ -10,7 +10,7 @@ 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; @@ -18,7 +18,6 @@ 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; diff --git a/objc2-foundation-sys/src/generated_others_shim.rs b/objc2-foundation-sys/src/generated_others_shim.rs index bcb42adec..94a843015 100644 --- a/objc2-foundation-sys/src/generated_others_shim.rs +++ b/objc2-foundation-sys/src/generated_others_shim.rs @@ -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; @@ -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) -> Option> { - Id::new_null(msg_send![self, initWithCoder: coder]) + pub unsafe fn initWithCoder_( + this: Id, + coder: NonNull, + ) -> Option> { + let this = ManuallyDrop::new(this); + Id::new_null(msg_send![this, initWithCoder: coder]) } pub unsafe fn count(&self) -> NSUInteger { msg_send![self, count] @@ -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::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, + bytes: *const c_void, + length: NSUInteger, + ) -> Id { + let this = ManuallyDrop::new(this); + Id::new(msg_send![this, initWithBytes: bytes, length: length]) + } + pub unsafe fn initWithBytesNoCopy_length_deallocator_( + this: Id, + bytes: *mut c_void, + length: NSUInteger, + deallocator: *mut c_void, + ) -> Id { + let this = ManuallyDrop::new(this); + Id::new(msg_send![ + this, + initWithBytesNoCopy: bytes, + length: length, + deallocator: deallocator, + ]) + } +} diff --git a/objc2-foundation/src/data.rs b/objc2-foundation/src/data.rs index 13a74d0f8..1149afd89 100644 --- a/objc2-foundation/src/data.rs +++ b/objc2-foundation/src/data.rs @@ -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; @@ -29,8 +29,12 @@ 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 { @@ -38,12 +42,12 @@ impl NSData { } 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()) } } }