|
|
|
@ -12,8 +12,11 @@ |
|
|
|
|
use std::marker::PhantomData; |
|
|
|
|
|
|
|
|
|
use crate::{ |
|
|
|
|
Mut, MutProxy, Proxied, SettableValue, View, ViewProxy, |
|
|
|
|
__internal::{Private, RawRepeatedField}, |
|
|
|
|
__runtime::{RepeatedField, RepeatedFieldInner}, |
|
|
|
|
primitive::PrimitiveMut, |
|
|
|
|
vtable::ProxiedWithRawVTable, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)] |
|
|
|
@ -31,6 +34,9 @@ pub struct RepeatedView<'a, T: ?Sized> { |
|
|
|
|
inner: RepeatedField<'a, T>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unsafe impl<'a, T: ProxiedWithRawVTable> Sync for RepeatedView<'a, T> {} |
|
|
|
|
unsafe impl<'a, T: ProxiedWithRawVTable> Send for RepeatedView<'a, T> {} |
|
|
|
|
|
|
|
|
|
impl<'msg, T: ?Sized> RepeatedView<'msg, T> { |
|
|
|
|
pub fn from_inner(_private: Private, inner: RepeatedFieldInner<'msg>) -> Self { |
|
|
|
|
Self { inner: RepeatedField::<'msg>::from_inner(_private, inner) } |
|
|
|
@ -49,14 +55,20 @@ impl<'a, T> std::fmt::Debug for RepeatedView<'a, T> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[repr(transparent)] |
|
|
|
|
#[derive(Debug)] |
|
|
|
|
pub struct RepeatedMut<'a, T: ?Sized> { |
|
|
|
|
inner: RepeatedField<'a, T>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unsafe impl<'a, T: ProxiedWithRawVTable> Sync for RepeatedMut<'a, T> {} |
|
|
|
|
|
|
|
|
|
impl<'msg, T: ?Sized> RepeatedMut<'msg, T> { |
|
|
|
|
pub fn from_inner(_private: Private, inner: RepeatedFieldInner<'msg>) -> Self { |
|
|
|
|
Self { inner: RepeatedField::from_inner(_private, inner) } |
|
|
|
|
} |
|
|
|
|
pub fn as_mut(&self) -> RepeatedMut<'_, T> { |
|
|
|
|
Self { inner: self.inner } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a, T> std::ops::Deref for RepeatedMut<'a, T> { |
|
|
|
@ -70,9 +82,67 @@ impl<'a, T> std::ops::Deref for RepeatedMut<'a, T> { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub struct RepeatedFieldIterMut<'a, T> { |
|
|
|
|
inner: RepeatedMut<'a, T>, |
|
|
|
|
current_index: usize, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub struct Repeated<T>(PhantomData<T>); |
|
|
|
|
|
|
|
|
|
macro_rules! impl_repeated_primitives { |
|
|
|
|
($($t:ty),*) => { |
|
|
|
|
$( |
|
|
|
|
impl Proxied for Repeated<$t> { |
|
|
|
|
type View<'a> = RepeatedView<'a, $t>; |
|
|
|
|
type Mut<'a> = RepeatedMut<'a, $t>; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> ViewProxy<'a> for RepeatedView<'a, $t> { |
|
|
|
|
type Proxied = Repeated<$t>; |
|
|
|
|
|
|
|
|
|
fn as_view(&self) -> View<'_, Self::Proxied> { |
|
|
|
|
*self |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied> |
|
|
|
|
where 'a: 'shorter, |
|
|
|
|
{ |
|
|
|
|
RepeatedView { inner: self.inner } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> ViewProxy<'a> for RepeatedMut<'a, $t> { |
|
|
|
|
type Proxied = Repeated<$t>; |
|
|
|
|
|
|
|
|
|
fn as_view(&self) -> View<'_, Self::Proxied> { |
|
|
|
|
**self |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied> |
|
|
|
|
where 'a: 'shorter, |
|
|
|
|
{ |
|
|
|
|
*self.into_mut::<'shorter>() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> MutProxy<'a> for RepeatedMut<'a, $t> { |
|
|
|
|
fn as_mut(&mut self) -> Mut<'_, Self::Proxied> { |
|
|
|
|
RepeatedMut { inner: self.inner } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn into_mut<'shorter>(self) -> Mut<'shorter, Self::Proxied> |
|
|
|
|
where 'a: 'shorter, |
|
|
|
|
{ |
|
|
|
|
RepeatedMut { inner: self.inner } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl <'a> SettableValue<Repeated<$t>> for RepeatedView<'a, $t> { |
|
|
|
|
fn set_on(self, _private: Private, mut mutator: Mut<'_, Repeated<$t>>) { |
|
|
|
|
mutator.copy_from(self); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> RepeatedView<'a, $t> { |
|
|
|
|
pub fn len(&self) -> usize { |
|
|
|
|
self.inner.len() |
|
|
|
@ -83,6 +153,9 @@ macro_rules! impl_repeated_primitives { |
|
|
|
|
pub fn get(&self, index: usize) -> Option<$t> { |
|
|
|
|
self.inner.get(index) |
|
|
|
|
} |
|
|
|
|
pub fn iter(&self) -> RepeatedFieldIter<'_, $t> { |
|
|
|
|
(*self).into_iter() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> RepeatedMut<'a, $t> { |
|
|
|
@ -92,6 +165,21 @@ macro_rules! impl_repeated_primitives { |
|
|
|
|
pub fn set(&mut self, index: usize, val: $t) { |
|
|
|
|
self.inner.set(index, val) |
|
|
|
|
} |
|
|
|
|
pub fn get_mut(&mut self, index: usize) -> Option<Mut<'_, $t>> { |
|
|
|
|
if index >= self.len() { |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
Some(PrimitiveMut::Repeated(self.as_mut(), index)) |
|
|
|
|
} |
|
|
|
|
pub fn iter(&self) -> RepeatedFieldIter<'_, $t> { |
|
|
|
|
self.as_view().into_iter() |
|
|
|
|
} |
|
|
|
|
pub fn iter_mut(&mut self) -> RepeatedFieldIterMut<'_, $t> { |
|
|
|
|
self.as_mut().into_iter() |
|
|
|
|
} |
|
|
|
|
pub fn copy_from(&mut self, src: RepeatedView<'_, $t>) { |
|
|
|
|
self.inner.copy_from(&src.inner); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> std::iter::Iterator for RepeatedFieldIter<'a, $t> { |
|
|
|
@ -112,6 +200,32 @@ macro_rules! impl_repeated_primitives { |
|
|
|
|
RepeatedFieldIter { inner: self.inner, current_index: 0 } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl <'a> std::iter::Iterator for RepeatedFieldIterMut<'a, $t> { |
|
|
|
|
type Item = Mut<'a, $t>; |
|
|
|
|
fn next(&mut self) -> Option<Self::Item> { |
|
|
|
|
if self.current_index >= self.inner.len() { |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
let elem = PrimitiveMut::Repeated( |
|
|
|
|
// While this appears to allow mutable aliasing
|
|
|
|
|
// (multiple `Self::Item`s can co-exist), each `Item`
|
|
|
|
|
// only references a specific unique index.
|
|
|
|
|
RepeatedMut{ inner: self.inner.inner }, |
|
|
|
|
self.current_index, |
|
|
|
|
); |
|
|
|
|
self.current_index += 1; |
|
|
|
|
Some(elem) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> std::iter::IntoIterator for RepeatedMut<'a, $t> { |
|
|
|
|
type Item = Mut<'a, $t>; |
|
|
|
|
type IntoIter = RepeatedFieldIterMut<'a, $t>; |
|
|
|
|
fn into_iter(self) -> Self::IntoIter { |
|
|
|
|
RepeatedFieldIterMut { inner: self, current_index: 0 } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
)* |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|