Skip to content

Commit

Permalink
Rename IdSlice[Mut] -> SliceId[Mut] and their methods
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Oct 15, 2021
1 parent 8c83d01 commit 183331a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
18 changes: 10 additions & 8 deletions objc2/src/rc/id_traits.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
//! Helper traits for Id.
use super::{Id, Owned, Ownership};
use crate::Message;

/// Helper trait for functionality on slices containing [`Id`]s.
pub trait IdSlice {
pub trait SliceId {
/// 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];
fn as_slice_mut(&mut self) -> &mut [&Self::Item];
}

/// Helper trait for functionality on slices containing owned [`Id`]s.
pub trait IdSliceMut {
pub trait SliceIdMut {
/// 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];
fn as_mut_slice_mut(&mut self) -> &mut [&mut Self::Item];
}

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

fn as_slice_ref(&self) -> &[&T] {
Expand All @@ -33,18 +35,18 @@ impl<T: Message, O: Ownership> IdSlice for [Id<T, O>] {
unsafe { &*ptr }
}

fn as_slice_mut_ref(&mut self) -> &mut [&T] {
fn as_slice_mut(&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>] {
impl<T: Message> SliceIdMut for [Id<T, Owned>] {
type Item = T;

fn as_mut_slice_mut_ref(&mut self) -> &mut [&mut T] {
fn as_mut_slice_mut(&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.
Expand Down
2 changes: 1 addition & 1 deletion objc2/src/rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod weak_id;

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

Expand Down
2 changes: 1 addition & 1 deletion 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, IdSlice, Owned, Ownership, Shared};
use objc2::rc::{Id, Owned, Ownership, Shared, SliceId};
use objc2::runtime::{Class, Object};
use objc2::{class, msg_send};
use objc2::{Encode, Encoding};
Expand Down
2 changes: 1 addition & 1 deletion 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, IdSlice, Owned, Ownership, Shared};
use objc2::rc::{Id, Owned, Ownership, Shared, SliceId};
use objc2::runtime::Class;
use objc2::{class, msg_send};

Expand Down

0 comments on commit 183331a

Please sign in to comment.