Skip to content

Commit

Permalink
Sync map accessors implementation from PR #700
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg committed Jun 25, 2024
1 parent 0db4bc5 commit d39fec5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
56 changes: 48 additions & 8 deletions protobuf/src/reflect/acc/v2/map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
Expand Down Expand Up @@ -29,17 +30,15 @@ impl<'a> fmt::Debug for MapFieldAccessorHolder {
}
}

struct MapFieldAccessorImpl<M, K, V>
struct MapFieldAccessorImpl<M, T>
where
M: MessageFull,
K: ProtobufValue,
V: ProtobufValue,
{
get_field: fn(&M) -> &HashMap<K, V>,
mut_field: fn(&mut M) -> &mut HashMap<K, V>,
get_field: fn(&M) -> &T,
mut_field: fn(&mut M) -> &mut T,
}

impl<M, K, V> MapFieldAccessor for MapFieldAccessorImpl<M, K, V>
impl<M, K, V> MapFieldAccessor for MapFieldAccessorImpl<M, HashMap<K, V>>
where
M: MessageFull,
K: ProtobufValue + Eq + Hash,
Expand All @@ -66,7 +65,35 @@ where
}
}

/// Make accessor for map field
impl<M, K, V> MapFieldAccessor for MapFieldAccessorImpl<M, BTreeMap<K, V>>
where
M: MessageFull,
K: ProtobufValue + Ord,
K::RuntimeType: RuntimeTypeMapKey,
V: ProtobufValue,
{
fn get_reflect<'a>(&self, m: &'a dyn MessageDyn) -> ReflectMapRef<'a> {
let m = m.downcast_ref().unwrap();
let map = (self.get_field)(m);
ReflectMapRef::new(map)
}

fn mut_reflect<'a>(&self, m: &'a mut dyn MessageDyn) -> ReflectMapMut<'a> {
let m = m.downcast_mut().unwrap();
let map = (self.mut_field)(m);
ReflectMapMut::new(map)
}

fn element_type(&self) -> (RuntimeType, RuntimeType) {
(
K::RuntimeType::runtime_type_box(),
V::RuntimeType::runtime_type_box(),
)
}
}

/// Make accessor for map field.
/// This is code generated by rust-protobuf before 3.5.
pub fn make_map_simpler_accessor<M, K, V>(
name: &'static str,
get_field: for<'a> fn(&'a M) -> &'a HashMap<K, V>,
Expand All @@ -77,11 +104,24 @@ where
K: ProtobufValue + Hash + Eq,
K::RuntimeType: RuntimeTypeMapKey,
V: ProtobufValue,
{
make_map_simpler_accessor_new(name, get_field, mut_field)
}

/// Make accessor for map field, new version
pub fn make_map_simpler_accessor_new<M, T>(

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / rustfmt check

trait `MapFieldAccessor` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / rustfmt check

type `MapFieldAccessorImpl<M, T>` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / Miri test

trait `reflect::acc::v2::map::MapFieldAccessor` is more private than the item `reflect::acc::v2::map::make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / Miri test

type `reflect::acc::v2::map::MapFieldAccessorImpl<M, T>` is more private than the item `reflect::acc::v2::map::make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux stable (with-bytes)

trait `MapFieldAccessor` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux stable (with-bytes)

type `MapFieldAccessorImpl<M, T>` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux stable (with-bytes)

trait `MapFieldAccessor` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux stable (with-bytes)

type `MapFieldAccessorImpl<M, T>` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux nightly (all features)

trait `MapFieldAccessor` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux nightly (all features)

type `MapFieldAccessorImpl<M, T>` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux stable (default features)

trait `MapFieldAccessor` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux stable (default features)

type `MapFieldAccessorImpl<M, T>` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux beta (default features)

trait `MapFieldAccessor` is more private than the item `make_map_simpler_accessor_new`

Check warning on line 112 in protobuf/src/reflect/acc/v2/map.rs

View workflow job for this annotation

GitHub Actions / linux beta (default features)

type `MapFieldAccessorImpl<M, T>` is more private than the item `make_map_simpler_accessor_new`
name: &'static str,
get_field: for<'a> fn(&'a M) -> &'a T,
mut_field: for<'a> fn(&'a mut M) -> &'a mut T,
) -> FieldAccessor
where
M: MessageFull,
MapFieldAccessorImpl<M, T>: MapFieldAccessor,
{
FieldAccessor::new(
name,
AccessorV2::Map(MapFieldAccessorHolder {
accessor: Box::new(MapFieldAccessorImpl::<M, K, V> {
accessor: Box::new(MapFieldAccessorImpl::<M, T> {
get_field,
mut_field,
}),
Expand Down
1 change: 1 addition & 0 deletions protobuf/src/reflect/rt/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![doc(hidden)]

pub use crate::reflect::acc::v2::map::make_map_simpler_accessor;
pub use crate::reflect::acc::v2::map::make_map_simpler_accessor_new;
pub use crate::reflect::acc::v2::repeated::make_vec_simpler_accessor;
pub use crate::reflect::acc::v2::singular::make_message_field_accessor;
pub use crate::reflect::acc::v2::singular::make_option_accessor;
Expand Down

0 comments on commit d39fec5

Please sign in to comment.