use crate::{
iter::{CopyIterator, RefIterator},
sync::Arc,
Stele,
};
use std::{
alloc::{Allocator, Global},
ops::Index,
};
pub struct ReadHandle<T, A: Allocator = Global> {
pub(crate) handle: Arc<Stele<T, A>>,
}
unsafe impl<T, A: Allocator> Send for ReadHandle<T, A> where Stele<T, A>: Send + Sync {}
unsafe impl<T, A: Allocator> Sync for ReadHandle<T, A> where Stele<T, A>: Sync {}
impl<T, A: Allocator> ReadHandle<T, A> {
pub fn read(&self, idx: usize) -> &T {
self.handle.read(idx)
}
pub fn try_read(&self, idx: usize) -> Option<&T> {
self.handle.try_read(idx)
}
pub fn len(&self) -> usize {
self.handle.len()
}
pub fn is_empty(&self) -> bool {
self.handle.is_empty()
}
}
impl<T: Copy, A: Allocator> ReadHandle<T, A> {
pub fn get(&self, idx: usize) -> T {
self.handle.get(idx)
}
}
impl<T, A: Allocator> Clone for ReadHandle<T, A> {
fn clone(&self) -> Self {
Self {
handle: Arc::clone(&self.handle),
}
}
}
impl<'a, T, A: Allocator> IntoIterator for &'a ReadHandle<T, A> {
type Item = &'a T;
type IntoIter = crate::iter::RefIterator<'a, T, A>;
fn into_iter(self) -> Self::IntoIter {
RefIterator::new(self)
}
}
impl<T: Copy, A: Allocator> IntoIterator for ReadHandle<T, A> {
type Item = T;
type IntoIter = crate::iter::CopyIterator<T, A>;
fn into_iter(self) -> Self::IntoIter {
CopyIterator::new(self)
}
}
impl<T, A: Allocator> Index<usize> for ReadHandle<T, A> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
self.read(index)
}
}