From 272fd53a7da9c5c0914730100e9933dfe52b40fe Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 21 Dec 2021 03:18:46 +0100 Subject: [PATCH] Add a few NSData methods --- .../src/generated_nsstring.rs | 3 +- .../src/generated_others_shim.rs | 58 +++++++++++++++++++ objc2-foundation/src/data.rs | 10 +++- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/objc2-foundation-sys/src/generated_nsstring.rs b/objc2-foundation-sys/src/generated_nsstring.rs index 81111a14d..8b3dc1e86 100644 --- a/objc2-foundation-sys/src/generated_nsstring.rs +++ b/objc2-foundation-sys/src/generated_nsstring.rs @@ -9,7 +9,7 @@ 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; @@ -17,7 +17,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 754fd292c..a7c183bc2 100644 --- a/objc2-foundation-sys/src/generated_others_shim.rs +++ b/objc2-foundation-sys/src/generated_others_shim.rs @@ -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; @@ -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, 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, Unknown>>, + bytes: *const c_void, + length: NSUInteger, + ) -> Id { + msg_send_id![this, initWithBytes: bytes, length: length].unwrap_unchecked() + } + pub unsafe fn initWithBytesNoCopy_length_deallocator_( + this: Option, Unknown>>, + bytes: *mut c_void, + length: NSUInteger, + deallocator: *mut c_void, + ) -> Id { + msg_send_id![ + this, + initWithBytesNoCopy: bytes, + length: length, + deallocator: deallocator, + ] + .unwrap_unchecked() + } +} diff --git a/objc2-foundation/src/data.rs b/objc2-foundation/src/data.rs index 846f80278..877a5c257 100644 --- a/objc2-foundation/src/data.rs +++ b/objc2-foundation/src/data.rs @@ -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. @@ -42,9 +42,13 @@ 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 { @@ -52,7 +56,7 @@ impl NSData { } 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() {