use super::{Id, Owned, Ownership};
use crate::Message;
pub trait SliceId {
type Item: ?Sized;
fn as_slice_ref(&self) -> &[&Self::Item];
fn as_slice_mut(&mut self) -> &mut [&Self::Item];
}
pub trait SliceIdMut: SliceId {
fn as_mut_slice_mut(&mut self) -> &mut [&mut Self::Item];
}
impl<T: Message + ?Sized, O: Ownership> SliceId for [Id<T, O>] {
type Item = T;
fn as_slice_ref(&self) -> &[&T] {
let ptr = self as *const Self as *const [&T];
unsafe { ptr.as_ref().unwrap_unchecked() }
}
fn as_slice_mut(&mut self) -> &mut [&T] {
let ptr = self as *mut Self as *mut [&T];
unsafe { ptr.as_mut().unwrap_unchecked() }
}
}
impl<T: Message + ?Sized> SliceIdMut for [Id<T, Owned>] {
fn as_mut_slice_mut(&mut self) -> &mut [&mut T] {
let ptr = self as *mut Self as *mut [&mut T];
unsafe { ptr.as_mut().unwrap_unchecked() }
}
}
pub trait DefaultId {
type Ownership: Ownership;
fn default_id() -> Id<Self, Self::Ownership>;
}
impl<T: DefaultId + ?Sized> Default for Id<T, T::Ownership> {
#[inline]
fn default() -> Self {
T::default_id()
}
}