Skip to content

Commit

Permalink
Re-add IdSlice trait
Browse files Browse the repository at this point in the history
Removed previously in 4118e96, but embedded drop flags are gone so this is no longer an issue.

Fixes SSheldon/rust-objc-id#1

Co-authored-by: Steven Sheldon <steven@sasheldon.com>
  • Loading branch information
madsmtm and SSheldon committed Oct 5, 2021
1 parent 9842d7b commit b6bca4b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
54 changes: 54 additions & 0 deletions objc2/src/rc/id_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::{Id, Owned, Ownership};
use crate::Message;

/// Helper trait for functionality on slices containing [`Id`]s.
pub trait IdSlice {
/// The type of the items in the slice.
type Item;

/// Convert a slice of [`Id`]s into a slice of references.
fn as_slice_ref(&self) -> &[&Self::Item];

/// Convert a mutable slice of [`Id`]s into a mutable slice of references.
fn as_slice_mut_ref(&mut self) -> &mut [&Self::Item];
}

/// Helper trait for functionality on slices containing owned [`Id`]s.
pub trait IdSliceMut {
/// The type of the items in the slice.
type Item;

/// Convert a mutable slice of mutable [`Id`]s into a mutable slice of
/// mutable references.
fn as_mut_slice_mut_ref(&mut self) -> &mut [&mut Self::Item];
}

impl<T: Message, O: Ownership> IdSlice for [Id<T, O>] {
type Item = T;

fn as_slice_ref(&self) -> &[&T] {
let ptr = self as *const Self as *const [&T];
// SAFETY: Id<T, O> and &T have the same memory layout. Further safety
// follows from `Deref` impl.
unsafe { &*ptr }
}

fn as_slice_mut_ref(&mut self) -> &mut [&T] {
let ptr = self as *mut Self as *mut [&T];
// SAFETY: Id<T, O> and &T have the same memory layout. Further safety
// follows from `Deref` impl.
unsafe { &mut *ptr }
}
}

impl<T: Message> IdSliceMut for [Id<T, Owned>] {
type Item = T;

fn as_mut_slice_mut_ref(&mut self) -> &mut [&mut T] {
let ptr = self as *mut Self as *mut [&mut T];
// SAFETY: Id<T, O> and &mut T have the same memory layout, and the
// `Id` is `Owned` so we're allowed to hand out mutable references.
// Further safety follows from `DerefMut` impl.
unsafe { &mut *ptr }
}
}
2 changes: 2 additions & 0 deletions objc2/src/rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@

mod autorelease;
mod id;
mod id_slice;
mod ownership;
mod weak_id;

pub use self::autorelease::{autoreleasepool, AutoreleasePool, AutoreleaseSafe};
pub use self::id::Id;
pub use self::id_slice::{IdSlice, IdSliceMut};
pub use self::ownership::{Owned, Ownership, Shared};
pub use self::weak_id::WeakId;

Expand Down
8 changes: 3 additions & 5 deletions objc2_foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::marker::PhantomData;
use core::ops::{Index, Range};
use core::ptr::NonNull;

use objc2::rc::{Id, Owned, Ownership, Shared};
use objc2::rc::{Id, IdSlice, Owned, Ownership, Shared};
use objc2::runtime::{Class, Object};
use objc2::{class, msg_send};
use objc2::{Encode, Encoding};
Expand Down Expand Up @@ -130,8 +130,7 @@ pub trait INSArray: INSObject {
}

fn from_vec(vec: Vec<Id<Self::Item, Self::Own>>) -> Id<Self, Owned> {
let refs: Vec<&Self::Item> = vec.iter().map(|obj| &**obj).collect();
unsafe { from_refs(&refs) }
unsafe { from_refs(vec.as_slice_ref()) }
}

fn objects_in_range(&self, range: Range<usize>) -> Vec<&Self::Item> {
Expand Down Expand Up @@ -178,8 +177,7 @@ pub trait INSArray: INSObject {
where
Self: INSArray<Own = Shared>,
{
let refs: Vec<&Self::Item> = slice.iter().map(|obj| &**obj).collect();
unsafe { from_refs(&refs) }
unsafe { from_refs(slice.as_slice_ref()) }
}

fn to_shared_vec(&self) -> Vec<Id<Self::Item, Shared>>
Expand Down
5 changes: 2 additions & 3 deletions objc2_foundation/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::marker::PhantomData;
use core::ops::Index;
use core::ptr::{self, NonNull};

use objc2::rc::{Id, Owned, Ownership, Shared};
use objc2::rc::{Id, IdSlice, Owned, Ownership, Shared};
use objc2::runtime::Class;
use objc2::{class, msg_send};

Expand Down Expand Up @@ -119,8 +119,7 @@ pub trait INSDictionary: INSObject {
where
T: INSCopying<Output = Self::Key>,
{
let vals_refs: Vec<&Self::Value> = vals.iter().map(|obj| &**obj).collect();
unsafe { from_refs(keys, &vals_refs) }
unsafe { from_refs(keys, &vals.as_slice_ref()) }
}

fn into_values_array(dict: Id<Self, Owned>) -> Id<NSArray<Self::Value, Self::Own>, Owned> {
Expand Down

0 comments on commit b6bca4b

Please sign in to comment.