Skip to content

Commit c3959b9

Browse files
Internal
PiperOrigin-RevId: 867793070
1 parent 0e4441f commit c3959b9

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

rust/map.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ use std::marker::PhantomData;
1818
#[doc(inline)]
1919
pub use crate::__internal::runtime::MapKey;
2020

21+
pub trait MapKeyValued {
22+
type MapKey: MapKey;
23+
type MapValue: MapValue;
24+
}
25+
26+
pub trait AsMapView: MapKeyValued {
27+
fn as_map_view(
28+
&self,
29+
) -> MapView<'_, <Self as MapKeyValued>::MapKey, <Self as MapKeyValued>::MapValue>;
30+
}
31+
32+
pub trait AsMapMut: MapKeyValued {
33+
fn as_map_mut(
34+
&mut self,
35+
) -> MapMut<'_, <Self as MapKeyValued>::MapKey, <Self as MapKeyValued>::MapValue>;
36+
}
37+
38+
pub trait IntoMap: MapKeyValued {
39+
fn into_map(self) -> Map<<Self as MapKeyValued>::MapKey, <Self as MapKeyValued>::MapValue>;
40+
}
41+
2142
/// A trait implemented by types which are allowed as values in maps, which is all Singular types.
2243
/// This trait is distinct from `Singular` only because of the generic `K: MapKey`.
2344
pub trait MapValue: Singular + SealedInternal {
@@ -129,6 +150,29 @@ impl<K: MapKey, V: MapValue> Proxied for Map<K, V> {
129150
type View<'msg> = MapView<'msg, K, V>;
130151
}
131152

153+
impl<K: MapKey, V: MapValue> MapKeyValued for Map<K, V> {
154+
type MapKey = K;
155+
type MapValue = V;
156+
}
157+
158+
impl<K: MapKey, V: MapValue> AsMapView for Map<K, V> {
159+
fn as_map_view(&self) -> MapView<'_, K, V> {
160+
self.as_view()
161+
}
162+
}
163+
164+
impl<K: MapKey, V: MapValue> AsMapMut for Map<K, V> {
165+
fn as_map_mut(&mut self) -> MapMut<'_, K, V> {
166+
self.as_mut()
167+
}
168+
}
169+
170+
impl<K: MapKey, V: MapValue> IntoMap for Map<K, V> {
171+
fn into_map(self) -> Map<K, V> {
172+
self
173+
}
174+
}
175+
132176
impl<K: MapKey, V: MapValue> AsView for Map<K, V> {
133177
type Proxied = Self;
134178

@@ -246,6 +290,34 @@ impl<'msg, K: MapKey, V: MapValue> MapView<'msg, K, V> {
246290
pub fn values(self) -> impl Iterator<Item = View<'msg, V>> + 'msg {
247291
self.into_iter().map(|(_, v)| v)
248292
}
293+
294+
/// Copies the contents of 'self' into a new owned Map.
295+
pub fn to_owned(self) -> Map<K, V>
296+
where
297+
View<'msg, V>: IntoProxied<V>,
298+
{
299+
self.into_proxied(Private)
300+
}
301+
}
302+
303+
impl<'msg, K: MapKey, V: MapValue> MapKeyValued for MapView<'msg, K, V> {
304+
type MapKey = K;
305+
type MapValue = V;
306+
}
307+
308+
impl<'msg, K: MapKey, V: MapValue> AsMapView for MapView<'msg, K, V> {
309+
fn as_map_view(&self) -> MapView<'_, K, V> {
310+
self.as_view()
311+
}
312+
}
313+
314+
impl<'msg, K: MapKey, V: MapValue> IntoMap for MapView<'msg, K, V>
315+
where
316+
View<'msg, V>: IntoProxied<V>,
317+
{
318+
fn into_map(self) -> Map<K, V> {
319+
self.to_owned()
320+
}
249321
}
250322

251323
impl<'msg, K: MapKey, V: MapValue> AsView for MapView<'msg, K, V> {
@@ -304,6 +376,32 @@ impl<'msg, K: ?Sized, V: ?Sized> std::fmt::Debug for MapMut<'msg, K, V> {
304376

305377
impl<'msg, K: MapKey, V: MapValue> SealedInternal for MapMut<'msg, K, V> {}
306378

379+
impl<'msg, K: MapKey, V: MapValue> MapKeyValued for MapMut<'msg, K, V> {
380+
type MapKey = K;
381+
type MapValue = V;
382+
}
383+
384+
impl<'msg, K: MapKey, V: MapValue> AsMapView for MapMut<'msg, K, V> {
385+
fn as_map_view(&self) -> MapView<'_, K, V> {
386+
self.as_view()
387+
}
388+
}
389+
390+
impl<'msg, K: MapKey, V: MapValue> AsMapMut for MapMut<'msg, K, V> {
391+
fn as_map_mut(&mut self) -> MapMut<'_, K, V> {
392+
self.as_mut()
393+
}
394+
}
395+
396+
impl<'msg, K: MapKey, V: MapValue> IntoMap for MapMut<'msg, K, V>
397+
where
398+
View<'msg, V>: IntoProxied<V>,
399+
{
400+
fn into_map(self) -> Map<K, V> {
401+
self.to_owned()
402+
}
403+
}
404+
307405
impl<'msg, K: MapKey, V: MapValue> AsView for MapMut<'msg, K, V> {
308406
type Proxied = Map<K, V>;
309407

@@ -419,6 +517,14 @@ impl<'msg, K: MapKey, V: MapValue> MapMut<'msg, K, V> {
419517
pub fn values(&self) -> impl Iterator<Item = View<'_, V>> + '_ {
420518
self.as_view().values()
421519
}
520+
521+
/// Copies the contents of 'self' into a new owned Map.
522+
pub fn to_owned(self) -> Map<K, V>
523+
where
524+
View<'msg, V>: IntoProxied<V>,
525+
{
526+
self.into_proxied(Private)
527+
}
422528
}
423529

424530
impl<'msg, K: MapKey, V: MapValue> IntoProxied<Map<K, V>> for MapMut<'msg, K, V>

0 commit comments

Comments
 (0)