Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions rust/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,29 @@ unsafe impl<K: MapKey, V: MapValue> Send for Map<K, V> {}
impl<K: MapKey, V: MapValue> SealedInternal for Map<K, V> {}

impl<K: MapKey, V: MapValue> Map<K, V> {
#[inline]
pub fn new() -> Self {
V::map_new(Private)
}

#[inline]
pub fn as_mut(&mut self) -> MapMut<'_, K, V> {
MapMut { inner: self.inner.as_mut(), _phantom: PhantomData }
}

#[inline]
pub fn as_view(&self) -> MapView<'_, K, V> {
MapView { raw: self.inner.raw, _phantom: PhantomData }
}

#[doc(hidden)]
#[inline]
pub fn from_inner(_: Private, inner: InnerMap) -> Self {
Self { inner, _phantom: PhantomData }
}

#[doc(hidden)]
#[inline]
pub fn as_raw(&self, _: Private) -> RawMap {
self.inner.raw
}
Expand Down Expand Up @@ -210,21 +215,25 @@ impl<'msg, K: ?Sized, V: ?Sized> MapView<'msg, K, V> {
}

impl<'msg, K: MapKey, V: MapValue> MapView<'msg, K, V> {
#[inline]
pub fn get<'a>(self, key: impl Into<View<'a, K>>) -> Option<View<'msg, V>>
where
K: 'a,
{
V::map_get(Private, self, key.into())
}

#[inline]
pub fn len(self) -> usize {
V::map_len(Private, self)
}

#[inline]
pub fn is_empty(self) -> bool {
self.len() == 0
}

#[inline]
pub fn contains_key<'a>(self, key: impl Into<View<'a, K>>) -> bool
where
K: 'a,
Expand Down Expand Up @@ -361,14 +370,17 @@ impl<'msg, K: ?Sized, V: ?Sized> MapMut<'msg, K, V> {
}

impl<'msg, K: MapKey, V: MapValue> MapMut<'msg, K, V> {
#[inline]
pub fn len(&self) -> usize {
self.as_view().len()
}

#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

#[inline]
pub fn contains_key<'a>(&self, key: impl Into<View<'a, K>>) -> bool
where
K: 'a,
Expand All @@ -379,22 +391,27 @@ impl<'msg, K: MapKey, V: MapValue> MapMut<'msg, K, V> {
/// Adds a key-value pair to the map.
///
/// Returns `true` if the entry was newly inserted.
#[inline]
pub fn insert<'a>(&mut self, key: impl Into<View<'a, K>>, value: impl IntoProxied<V>) -> bool {
V::map_insert(Private, self.as_mut(), key.into(), value)
}

#[inline]
pub fn remove<'a>(&mut self, key: impl Into<View<'a, K>>) -> bool {
V::map_remove(Private, self.as_mut(), key.into())
}

#[inline]
pub fn clear(&mut self) {
V::map_clear(Private, self.as_mut())
}

#[inline]
pub fn get<'a>(&self, key: impl Into<View<'a, K>>) -> Option<View<'_, V>> {
V::map_get(Private, self.as_view(), key.into())
}

#[inline]
pub fn get_mut<'a>(&mut self, key: impl Into<View<'a, K>>) -> Option<Mut<'_, V>>
where
V: Message,
Expand All @@ -416,20 +433,23 @@ impl<'msg, K: MapKey, V: MapValue> MapMut<'msg, K, V> {
/// Returns an iterator visiting all key-value pairs in arbitrary order.
///
/// The iterator element type is `(View<K>, View<V>)`.
#[inline]
pub fn iter(&self) -> MapIter<'_, K, V> {
self.into_iter()
}

/// Returns an iterator visiting all keys in arbitrary order.
///
/// The iterator element type is `View<K>`.
#[inline]
pub fn keys(&self) -> impl Iterator<Item = View<'_, K>> + '_ {
self.as_view().keys()
}

/// Returns an iterator visiting all values in arbitrary order.
///
/// The iterator element type is `View<V>`.
#[inline]
pub fn values(&self) -> impl Iterator<Item = View<'_, V>> + '_ {
self.as_view().values()
}
Expand Down
6 changes: 6 additions & 0 deletions src/google/protobuf/compiler/rust/accessors/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field,
[&] {
if (ctx.is_upb()) {
ctx.Emit(R"rs(
#[inline]
pub fn $field$($view_self$)
-> $pb$::MapView<$view_lifetime$, $Key$, $Value$> {
unsafe {
Expand All @@ -68,6 +69,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field,
})rs");
} else {
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")}}, R"rs(
#[inline]
pub fn $field$($view_self$)
-> $pb$::MapView<$view_lifetime$, $Key$, $Value$> {
unsafe {
Expand All @@ -84,6 +86,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
#[inline]
pub fn $field$_mut(&mut self)
-> $pb$::MapMut<'_, $Key$, $Value$> {
unsafe {
Expand All @@ -98,6 +101,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field,
} else {
ctx.Emit({{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}},
R"rs(
#[inline]
pub fn $field$_mut(&mut self)
-> $pb$::MapMut<'_, $Key$, $Value$> {
let inner = $pbr$::InnerMapMut::new(
Expand All @@ -113,6 +117,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
#[inline]
pub fn set_$raw_field_name$(
&mut self,
src: impl $pb$::IntoProxied<$pb$::Map<$Key$, $Value$>>) {
Expand All @@ -127,6 +132,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field,
} else {
ctx.Emit({{"move_setter_thunk", ThunkName(ctx, field, "set")}},
R"rs(
#[inline]
pub fn set_$raw_field_name$(
&mut self,
src: impl $pb$::IntoProxied<$pb$::Map<$Key$, $Value$>>) {
Expand Down
Loading