@ -22,44 +22,44 @@ use crate::{
/// Views the elements in a `repeated` field of `T`.
/// Views the elements in a `repeated` field of `T`.
#[ repr(transparent) ]
#[ repr(transparent) ]
pub struct RepeatedView < ' msg , T : ? Sized > {
pub struct RepeatedView < ' msg , T > {
// This does not need to carry an arena in upb, so it can be just the raw repeated field
// This does not need to carry an arena in upb, so it can be just the raw repeated field
raw : RawRepeatedField ,
raw : RawRepeatedField ,
_phantom : PhantomData < & ' msg T > ,
_phantom : PhantomData < & ' msg T > ,
}
}
impl < ' msg , T : ? Sized > Copy for RepeatedView < ' msg , T > { }
impl < ' msg , T > Copy for RepeatedView < ' msg , T > { }
impl < ' msg , T : ? Sized > Clone for RepeatedView < ' msg , T > {
impl < ' msg , T > Clone for RepeatedView < ' msg , T > {
fn clone ( & self ) -> Self {
fn clone ( & self ) -> Self {
* self
* self
}
}
}
}
unsafe impl < ' msg , T : ? Sized > Sync for RepeatedView < ' msg , T > { }
unsafe impl < ' msg , T > Sync for RepeatedView < ' msg , T > { }
unsafe impl < ' msg , T : ? Sized > Send for RepeatedView < ' msg , T > { }
unsafe impl < ' msg , T > Send for RepeatedView < ' msg , T > { }
impl < ' msg , T : ? Sized > Debug for RepeatedView < ' msg , T > {
impl < ' msg , T > Debug for RepeatedView < ' msg , T > {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
f . debug_struct ( "RepeatedView" ) . field ( "raw" , & self . raw ) . finish ( )
f . debug_struct ( "RepeatedView" ) . field ( "raw" , & self . raw ) . finish ( )
}
}
}
}
/// Mutates the elements in a `repeated` field of `T`.
/// Mutates the elements in a `repeated` field of `T`.
pub struct RepeatedMut < ' msg , T : ? Sized > {
pub struct RepeatedMut < ' msg , T > {
pub ( crate ) inner : InnerRepeatedMut < ' msg > ,
pub ( crate ) inner : InnerRepeatedMut < ' msg > ,
_phantom : PhantomData < & ' msg mut T > ,
_phantom : PhantomData < & ' msg mut T > ,
}
}
unsafe impl < ' msg , T : ? Sized > Sync for RepeatedMut < ' msg , T > { }
unsafe impl < ' msg , T > Sync for RepeatedMut < ' msg , T > { }
impl < ' msg , T : ? Sized > Debug for RepeatedMut < ' msg , T > {
impl < ' msg , T > Debug for RepeatedMut < ' msg , T > {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
f . debug_struct ( "RepeatedMut" ) . field ( "raw" , & self . inner . raw ) . finish ( )
f . debug_struct ( "RepeatedMut" ) . field ( "raw" , & self . inner . raw ) . finish ( )
}
}
}
}
#[ doc(hidden) ]
#[ doc(hidden) ]
impl < ' msg , T : ? Sized > RepeatedView < ' msg , T > {
impl < ' msg , T > RepeatedView < ' msg , T > {
#[ doc(hidden) ]
#[ doc(hidden) ]
#[ inline ]
#[ inline ]
pub fn as_raw ( & self , _private : Private ) -> RawRepeatedField {
pub fn as_raw ( & self , _private : Private ) -> RawRepeatedField {
@ -77,7 +77,7 @@ impl<'msg, T: ?Sized> RepeatedView<'msg, T> {
impl < ' msg , T > RepeatedView < ' msg , T >
impl < ' msg , T > RepeatedView < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
/// Gets the length of the repeated field.
/// Gets the length of the repeated field.
#[ inline ]
#[ inline ]
@ -120,7 +120,7 @@ where
}
}
#[ doc(hidden) ]
#[ doc(hidden) ]
impl < ' msg , T : ? Sized > RepeatedMut < ' msg , T > {
impl < ' msg , T > RepeatedMut < ' msg , T > {
/// # Safety
/// # Safety
/// - `inner` must be valid to read and write from for `'msg`
/// - `inner` must be valid to read and write from for `'msg`
/// - There must be no aliasing references or mutations on the same
/// - There must be no aliasing references or mutations on the same
@ -140,7 +140,7 @@ impl<'msg, T: ?Sized> RepeatedMut<'msg, T> {
impl < ' msg , T > RepeatedMut < ' msg , T >
impl < ' msg , T > RepeatedMut < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
/// Gets the length of the repeated field.
/// Gets the length of the repeated field.
#[ inline ]
#[ inline ]
@ -174,8 +174,7 @@ where
/// Appends `val` to the end of the repeated field.
/// Appends `val` to the end of the repeated field.
#[ inline ]
#[ inline ]
pub fn push ( & mut self , val : View < T > ) {
pub fn push ( & mut self , val : impl IntoProxied < T > ) {
// TODO: b/320936046 - Use SettableValue instead of View for added ergonomics.
T ::repeated_push ( self . as_mut ( ) , val ) ;
T ::repeated_push ( self . as_mut ( ) , val ) ;
}
}
@ -184,13 +183,11 @@ where
/// # Panics
/// # Panics
/// Panics if `index >= len`
/// Panics if `index >= len`
#[ inline ]
#[ inline ]
pub fn set ( & mut self , index : usize , val : View < T > ) {
pub fn set ( & mut self , index : usize , val : impl IntoProxied < T > ) {
let len = self . len ( ) ;
let len = self . len ( ) ;
if index > = len {
if index > = len {
panic! ( "index {index} >= repeated len {len}" ) ;
panic! ( "index {index} >= repeated len {len}" ) ;
}
}
// TODO: b/320936046 - Use SettableValue instead of View for added ergonomics.
// SAFETY: `index` has been checked to be in-bounds.
unsafe { self . set_unchecked ( index , val ) }
unsafe { self . set_unchecked ( index , val ) }
}
}
@ -199,9 +196,7 @@ where
/// # Safety
/// # Safety
/// Undefined behavior if `index >= len`
/// Undefined behavior if `index >= len`
#[ inline ]
#[ inline ]
pub unsafe fn set_unchecked ( & mut self , index : usize , val : View < T > ) {
pub unsafe fn set_unchecked ( & mut self , index : usize , val : impl IntoProxied < T > ) {
// TODO: b/320936046 - Use SettableValue instead of View for added ergonomics.
// SAFETY: `index` is in-bounds as promised by the caller.
unsafe { T ::repeated_set_unchecked ( self . as_mut ( ) , index , val ) }
unsafe { T ::repeated_set_unchecked ( self . as_mut ( ) , index , val ) }
}
}
@ -225,7 +220,7 @@ where
impl < T > Repeated < T >
impl < T > Repeated < T >
where
where
T : ? Sized + ProxiedInRepeated ,
T : ProxiedInRepeated ,
{
{
pub fn as_view ( & self ) -> View < Repeated < T > > {
pub fn as_view ( & self ) -> View < Repeated < T > > {
RepeatedView { raw : self . inner . raw ( ) , _phantom : PhantomData }
RepeatedView { raw : self . inner . raw ( ) , _phantom : PhantomData }
@ -239,7 +234,7 @@ where
impl < T > IntoProxied < Repeated < T > > for Repeated < T >
impl < T > IntoProxied < Repeated < T > > for Repeated < T >
where
where
T : ? Sized + ProxiedInRepeated ,
T : ProxiedInRepeated ,
{
{
fn into_proxied ( self , _private : Private ) -> Repeated < T > {
fn into_proxied ( self , _private : Private ) -> Repeated < T > {
self
self
@ -248,7 +243,7 @@ where
impl < ' msg , T > IntoProxied < Repeated < T > > for RepeatedView < ' msg , T >
impl < ' msg , T > IntoProxied < Repeated < T > > for RepeatedView < ' msg , T >
where
where
T : ' msg + ? Sized + ProxiedInRepeated ,
T : ' msg + ProxiedInRepeated ,
{
{
fn into_proxied ( self , _private : Private ) -> Repeated < T > {
fn into_proxied ( self , _private : Private ) -> Repeated < T > {
let mut repeated : Repeated < T > = Repeated ::new ( ) ;
let mut repeated : Repeated < T > = Repeated ::new ( ) ;
@ -259,7 +254,7 @@ where
impl < ' msg , T > IntoProxied < Repeated < T > > for RepeatedMut < ' msg , T >
impl < ' msg , T > IntoProxied < Repeated < T > > for RepeatedMut < ' msg , T >
where
where
T : ' msg + ? Sized + ProxiedInRepeated ,
T : ' msg + ProxiedInRepeated ,
{
{
fn into_proxied ( self , _private : Private ) -> Repeated < T > {
fn into_proxied ( self , _private : Private ) -> Repeated < T > {
IntoProxied ::into_proxied ( self . as_view ( ) , _private )
IntoProxied ::into_proxied ( self . as_view ( ) , _private )
@ -296,7 +291,7 @@ pub unsafe trait ProxiedInRepeated: Proxied {
fn repeated_len ( repeated : View < Repeated < Self > > ) -> usize ;
fn repeated_len ( repeated : View < Repeated < Self > > ) -> usize ;
/// Appends a new element to the end of the repeated field.
/// Appends a new element to the end of the repeated field.
fn repeated_push ( repeated : Mut < Repeated < Self > > , val : View < Self > ) ;
fn repeated_push ( repeated : Mut < Repeated < Self > > , val : impl IntoProxied < Self > ) ;
/// Clears the repeated field of elements.
/// Clears the repeated field of elements.
fn repeated_clear ( repeated : Mut < Repeated < Self > > ) ;
fn repeated_clear ( repeated : Mut < Repeated < Self > > ) ;
@ -307,7 +302,11 @@ pub unsafe trait ProxiedInRepeated: Proxied {
/// # Safety
/// # Safety
/// `index` must be less than `Self::repeated_len(repeated)`
/// `index` must be less than `Self::repeated_len(repeated)`
unsafe fn repeated_set_unchecked ( repeated : Mut < Repeated < Self > > , index : usize , val : View < Self > ) ;
unsafe fn repeated_set_unchecked (
repeated : Mut < Repeated < Self > > ,
index : usize ,
val : impl IntoProxied < Self > ,
) ;
/// Copies the values in the `src` repeated field into `dest`.
/// Copies the values in the `src` repeated field into `dest`.
fn repeated_copy_from ( src : View < Repeated < Self > > , dest : Mut < Repeated < Self > > ) ;
fn repeated_copy_from ( src : View < Repeated < Self > > , dest : Mut < Repeated < Self > > ) ;
@ -318,12 +317,12 @@ pub unsafe trait ProxiedInRepeated: Proxied {
}
}
/// An iterator over the values inside of a [`View<Repeated<T>>`](RepeatedView).
/// An iterator over the values inside of a [`View<Repeated<T>>`](RepeatedView).
pub struct RepeatedIter < ' msg , T : ? Sized > {
pub struct RepeatedIter < ' msg , T > {
view : RepeatedView < ' msg , T > ,
view : RepeatedView < ' msg , T > ,
current_index : usize ,
current_index : usize ,
}
}
impl < ' msg , T : ? Sized > Debug for RepeatedIter < ' msg , T > {
impl < ' msg , T > Debug for RepeatedIter < ' msg , T > {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
f . debug_struct ( "RepeatedIter" )
f . debug_struct ( "RepeatedIter" )
. field ( "view" , & self . view )
. field ( "view" , & self . view )
@ -336,19 +335,19 @@ impl<'msg, T: ?Sized> Debug for RepeatedIter<'msg, T> {
///
///
/// Users will generally write [`View<Repeated<T>>`](RepeatedView) or
/// Users will generally write [`View<Repeated<T>>`](RepeatedView) or
/// [`Mut<Repeated<T>>`](RepeatedMut) to access the repeated elements
/// [`Mut<Repeated<T>>`](RepeatedMut) to access the repeated elements
pub struct Repeated < T : ? Sized + ProxiedInRepeated > {
pub struct Repeated < T : ProxiedInRepeated > {
pub ( crate ) inner : InnerRepeated ,
pub ( crate ) inner : InnerRepeated ,
_phantom : PhantomData < T > ,
_phantom : PhantomData < T > ,
}
}
// SAFETY: `Repeated` is Sync because it does not implement interior mutability.
// SAFETY: `Repeated` is Sync because it does not implement interior mutability.
unsafe impl < T : ? Sized + ProxiedInRepeated > Sync for Repeated < T > { }
unsafe impl < T : ProxiedInRepeated > Sync for Repeated < T > { }
// SAFETY: `Repeated` is Send because it's not bound to a specific thread e.g.
// SAFETY: `Repeated` is Send because it's not bound to a specific thread e.g.
// it does not use thread-local data or similar.
// it does not use thread-local data or similar.
unsafe impl < T : ? Sized + ProxiedInRepeated > Send for Repeated < T > { }
unsafe impl < T : ProxiedInRepeated > Send for Repeated < T > { }
impl < T : ? Sized + ProxiedInRepeated > Repeated < T > {
impl < T : ProxiedInRepeated > Repeated < T > {
pub fn new ( ) -> Self {
pub fn new ( ) -> Self {
T ::repeated_new ( Private )
T ::repeated_new ( Private )
}
}
@ -362,13 +361,13 @@ impl<T: ?Sized + ProxiedInRepeated> Repeated<T> {
}
}
}
}
impl < T : ? Sized + ProxiedInRepeated > Default for Repeated < T > {
impl < T : ProxiedInRepeated > Default for Repeated < T > {
fn default ( ) -> Self {
fn default ( ) -> Self {
Repeated ::new ( )
Repeated ::new ( )
}
}
}
}
impl < T : ? Sized + ProxiedInRepeated > Drop for Repeated < T > {
impl < T : ProxiedInRepeated > Drop for Repeated < T > {
fn drop ( & mut self ) {
fn drop ( & mut self ) {
// SAFETY: only called once
// SAFETY: only called once
unsafe { T ::repeated_free ( Private , self ) }
unsafe { T ::repeated_free ( Private , self ) }
@ -377,21 +376,21 @@ impl<T: ?Sized + ProxiedInRepeated> Drop for Repeated<T> {
impl < T > Proxied for Repeated < T >
impl < T > Proxied for Repeated < T >
where
where
T : ProxiedInRepeated + ? Sized ,
T : ProxiedInRepeated ,
{
{
type View < ' msg > = RepeatedView < ' msg , T > where Repeated < T > : ' msg ;
type View < ' msg > = RepeatedView < ' msg , T > where Repeated < T > : ' msg ;
}
}
impl < T > MutProxied for Repeated < T >
impl < T > MutProxied for Repeated < T >
where
where
T : ProxiedInRepeated + ? Sized ,
T : ProxiedInRepeated ,
{
{
type Mut < ' msg > = RepeatedMut < ' msg , T > where Repeated < T > : ' msg ;
type Mut < ' msg > = RepeatedMut < ' msg , T > where Repeated < T > : ' msg ;
}
}
impl < ' msg , T > ViewProxy < ' msg > for RepeatedView < ' msg , T >
impl < ' msg , T > ViewProxy < ' msg > for RepeatedView < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
type Proxied = Repeated < T > ;
type Proxied = Repeated < T > ;
@ -411,7 +410,7 @@ where
impl < ' msg , T > ViewProxy < ' msg > for RepeatedMut < ' msg , T >
impl < ' msg , T > ViewProxy < ' msg > for RepeatedMut < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
type Proxied = Repeated < T > ;
type Proxied = Repeated < T > ;
@ -431,7 +430,7 @@ where
impl < ' msg , T > MutProxy < ' msg > for RepeatedMut < ' msg , T >
impl < ' msg , T > MutProxy < ' msg > for RepeatedMut < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
#[ inline ]
#[ inline ]
fn as_mut ( & mut self ) -> Mut < ' _ , Self ::Proxied > {
fn as_mut ( & mut self ) -> Mut < ' _ , Self ::Proxied > {
@ -449,7 +448,7 @@ where
impl < ' msg , T > iter ::Iterator for RepeatedIter < ' msg , T >
impl < ' msg , T > iter ::Iterator for RepeatedIter < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
type Item = View < ' msg , T > ;
type Item = View < ' msg , T > ;
@ -468,18 +467,18 @@ where
}
}
}
}
impl < ' msg , T : ? Sized + ProxiedInRepeated > ExactSizeIterator for RepeatedIter < ' msg , T > {
impl < ' msg , T : ProxiedInRepeated > ExactSizeIterator for RepeatedIter < ' msg , T > {
fn len ( & self ) -> usize {
fn len ( & self ) -> usize {
self . view . len ( ) - self . current_index
self . view . len ( ) - self . current_index
}
}
}
}
// TODO: impl DoubleEndedIterator
// TODO: impl DoubleEndedIterator
impl < ' msg , T : ? Sized + ProxiedInRepeated > FusedIterator for RepeatedIter < ' msg , T > { }
impl < ' msg , T : ProxiedInRepeated > FusedIterator for RepeatedIter < ' msg , T > { }
impl < ' msg , T > iter ::IntoIterator for RepeatedView < ' msg , T >
impl < ' msg , T > iter ::IntoIterator for RepeatedView < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
type Item = View < ' msg , T > ;
type Item = View < ' msg , T > ;
type IntoIter = RepeatedIter < ' msg , T > ;
type IntoIter = RepeatedIter < ' msg , T > ;
@ -491,7 +490,7 @@ where
impl < ' msg , T > iter ::IntoIterator for & ' _ RepeatedView < ' msg , T >
impl < ' msg , T > iter ::IntoIterator for & ' _ RepeatedView < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' msg ,
T : ProxiedInRepeated + ' msg ,
{
{
type Item = View < ' msg , T > ;
type Item = View < ' msg , T > ;
type IntoIter = RepeatedIter < ' msg , T > ;
type IntoIter = RepeatedIter < ' msg , T > ;
@ -503,7 +502,7 @@ where
impl < ' borrow , T > iter ::IntoIterator for & ' borrow RepeatedMut < ' _ , T >
impl < ' borrow , T > iter ::IntoIterator for & ' borrow RepeatedMut < ' _ , T >
where
where
T : ProxiedInRepeated + ? Sized + ' borrow ,
T : ProxiedInRepeated + ' borrow ,
{
{
type Item = View < ' borrow , T > ;
type Item = View < ' borrow , T > ;
type IntoIter = RepeatedIter < ' borrow , T > ;
type IntoIter = RepeatedIter < ' borrow , T > ;
@ -515,14 +514,14 @@ where
impl < ' msg , ' view , T , ViewT > Extend < ViewT > for RepeatedMut < ' msg , T >
impl < ' msg , ' view , T , ViewT > Extend < ViewT > for RepeatedMut < ' msg , T >
where
where
T : ProxiedInRepeated + ? Sized + ' view ,
T : ProxiedInRepeated + ' view ,
ViewT : Into < View < ' view , T > > ,
ViewT : IntoProxied < T > ,
{
{
fn extend < I : IntoIterator < Item = ViewT > > ( & mut self , iter : I ) {
fn extend < I : IntoIterator < Item = ViewT > > ( & mut self , iter : I ) {
let iter = iter . into_iter ( ) ;
let iter = iter . into_iter ( ) ;
T ::repeated_reserve ( self . as_mut ( ) , iter . size_hint ( ) . 0 ) ;
T ::repeated_reserve ( self . as_mut ( ) , iter . size_hint ( ) . 0 ) ;
for item in iter {
for item in iter {
self . push ( item . into ( ) ) ;
self . push ( item ) ;
}
}
}
}
}
}