Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better follow API guidelines #102

Merged
merged 12 commits into from
Jan 2, 2022
Prev Previous commit
Next Next commit
Use AsRef, AsMut and Index
madsmtm committed Jan 2, 2022

Verified

This commit was signed with the committer’s verified signature. The key has expired.
kmbcook Kevin Cook
commit 0eba2af9e366d6b8bfd8f52725ea639e3b1e158b
2 changes: 2 additions & 0 deletions objc2-foundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Implement `PartialOrd` and `Ord` for `NSComparisonResult` and `NSValue`.
* Implement `fmt::Display` for `NSValue`.
* Implement `DefaultId` for relevant objects.
* Implement `AsRef` and `Index` for `NSData` and `NSMutableData`.
* Implement `AsMut` and `IndexMut` for `NSMutableData`.

### Changed
* **BREAKING**: Renamed `INSFastEnumeration::enumerator` to
8 changes: 7 additions & 1 deletion objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use alloc::vec::Vec;
use core::cmp::Ordering;
use core::ffi::c_void;
use core::marker::PhantomData;
use core::ops::{Index, Range};
use core::ops::{Index, IndexMut, Range};
use core::ptr::NonNull;

use objc2::msg_send;
@@ -387,6 +387,12 @@ impl<T: INSObject, O: Ownership> Index<usize> for NSMutableArray<T, O> {
}
}

impl<T: INSObject> IndexMut<usize> for NSMutableArray<T, Owned> {
fn index_mut(&mut self, index: usize) -> &mut T {
self.get_mut(index).unwrap()
}
}

impl<T: INSObject, O: Ownership> DefaultId for NSMutableArray<T, O> {
type Ownership = Owned;

47 changes: 45 additions & 2 deletions objc2-foundation/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "block")]
use alloc::vec::Vec;
use core::ops::{Deref, DerefMut, Range};
use core::slice;
use core::ops::{Deref, DerefMut, Index, IndexMut, Range};
use core::slice::{self, SliceIndex};
use core::{ffi::c_void, ptr::NonNull};

use super::{INSCopying, INSMutableCopying, INSObject, NSRange};
@@ -111,6 +111,21 @@ unsafe impl INSMutableCopying for NSData {
type Output = NSMutableData;
}

impl AsRef<[u8]> for NSData {
fn as_ref(&self) -> &[u8] {
self.bytes()
}
}

impl<I: SliceIndex<[u8]>> Index<I> for NSData {
type Output = I::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
Index::index(self.bytes(), index)
}
}

impl DefaultId for NSData {
type Ownership = Shared;

@@ -189,6 +204,34 @@ unsafe impl INSMutableCopying for NSMutableData {
type Output = NSMutableData;
}

impl AsRef<[u8]> for NSMutableData {
fn as_ref(&self) -> &[u8] {
self.bytes()
}
}

impl AsMut<[u8]> for NSMutableData {
fn as_mut(&mut self) -> &mut [u8] {
self.bytes_mut()
}
}

impl<I: SliceIndex<[u8]>> Index<I> for NSMutableData {
type Output = I::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
Index::index(self.bytes(), index)
}
}

impl<I: SliceIndex<[u8]>> IndexMut<I> for NSMutableData {
#[inline]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
IndexMut::index_mut(self.bytes_mut(), index)
}
}

impl DefaultId for NSMutableData {
type Ownership = Owned;

2 changes: 1 addition & 1 deletion objc2-foundation/src/dictionary.rs
Original file line number Diff line number Diff line change
@@ -175,7 +175,7 @@ unsafe impl<K: INSObject, V: INSObject> INSFastEnumeration for NSDictionary<K, V
impl<'a, K: INSObject, V: INSObject> Index<&'a K> for NSDictionary<K, V> {
type Output = V;

fn index(&self, index: &K) -> &V {
fn index<'s>(&'s self, index: &'a K) -> &'s V {
self.get(index).unwrap()
}
}