Name the lifetime in the signature of SettableValue.set_on()

This change names the lifetime of Mut<'a, T> and requires that T outlives 'a. The motivation for this change came up while implementing `Map<K, ProtoStr>`. The Map implementation makes it so that `V` needs to implement the `MapWithKeyOps` trait which has an associated type with a lifetime (`Value<'a>`. The lifetime bound on `T` ensures that e.g. for `MapWithKeyOps<Value<'b>=&'b ProtoStr>` `'a` outlives `'b`.

PiperOrigin-RevId: 585657154
pull/14853/head
Jakob Buchgraber 1 year ago committed by Copybara-Service
parent f9929e997e
commit ab11a0d1a8
  1. 6
      rust/macros.rs
  2. 4
      rust/map.rs
  3. 10
      rust/optional.rs
  4. 2
      rust/primitive.rs
  5. 24
      rust/proxied.rs
  6. 4
      rust/repeated.rs
  7. 10
      rust/string.rs
  8. 4
      src/google/protobuf/compiler/rust/message.cc

@ -19,11 +19,11 @@
/// ```
macro_rules! impl_forwarding_settable_value {
($proxied:ty, $self:ident => $self_forwarding_expr:expr) => {
fn set_on(
fn set_on<'b>(
$self,
_private: $crate::__internal::Private,
mutator: $crate::Mut<'_, $proxied>,
) {
mutator: $crate::Mut<'b, $proxied>,
) where $proxied: 'b {
($self_forwarding_expr).set_on(Private, mutator)
}

@ -78,7 +78,9 @@ macro_rules! impl_scalar_map_keys {
}
impl<'a, V: [< MapWith $t:camel KeyOps >]> SettableValue<Map<$t, V>> for MapView<'a, $t, V> {
fn set_on(self, _private: Private, mut mutator: Mut<'_, Map<$t, V>>) {
fn set_on<'b>(self, _private: Private, mut mutator: Mut<'b, Map<$t, V>>)
where
Map<$t, V>: 'b {
mutator.copy_from(self);
}
}

@ -589,7 +589,10 @@ mod tests {
}
impl SettableValue<VtableProxied> for View<'_, VtableProxied> {
fn set_on(self, _private: Private, mutator: Mut<VtableProxied>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, VtableProxied>)
where
VtableProxied: 'a,
{
SettableValue::<VtableProxied>::set_on(self.val(), Private, mutator)
}
@ -603,7 +606,10 @@ mod tests {
}
impl SettableValue<VtableProxied> for i32 {
fn set_on(self, _private: Private, mutator: Mut<VtableProxied>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, VtableProxied>)
where
VtableProxied: 'a,
{
(mutator.vtable.set)(mutator.msg, self)
}

@ -116,7 +116,7 @@ macro_rules! impl_singular_primitives {
}
impl SettableValue<$t> for $t {
fn set_on(self, _private: Private, mutator: Mut<'_, $t>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, $t>) where $t: 'a {
match mutator {
PrimitiveMut::Singular(s) => {
unsafe { (s.inner).set(self) };

@ -239,7 +239,9 @@ where
{
/// Consumes `self` to set the given mutator to its value.
#[doc(hidden)]
fn set_on(self, _private: Private, mutator: Mut<'_, T>);
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, T>)
where
T: 'a;
/// Consumes `self` and `absent_mutator` to set the given empty field to
/// a value.
@ -352,25 +354,37 @@ mod tests {
}
impl SettableValue<MyProxied> for MyProxiedView<'_> {
fn set_on(self, _private: Private, mutator: Mut<MyProxied>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, MyProxied>)
where
MyProxied: 'a,
{
mutator.my_proxied_ref.val = self.my_proxied_ref.val.clone();
}
}
impl SettableValue<MyProxied> for String {
fn set_on(self, _private: Private, mutator: Mut<MyProxied>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, MyProxied>)
where
MyProxied: 'a,
{
mutator.my_proxied_ref.val = self;
}
}
impl SettableValue<MyProxied> for &'_ str {
fn set_on(self, _private: Private, mutator: Mut<MyProxied>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, MyProxied>)
where
MyProxied: 'a,
{
mutator.my_proxied_ref.val.replace_range(.., self);
}
}
impl SettableValue<MyProxied> for Cow<'_, str> {
fn set_on(self, _private: Private, mutator: Mut<MyProxied>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, MyProxied>)
where
MyProxied: 'a,
{
match self {
Cow::Owned(x) => <String as SettableValue<MyProxied>>::set_on(x, Private, mutator),
Cow::Borrowed(x) => <&str as SettableValue<MyProxied>>::set_on(x, Private, mutator),

@ -138,7 +138,9 @@ macro_rules! impl_repeated_primitives {
}
impl <'a> SettableValue<Repeated<$t>> for RepeatedView<'a, $t> {
fn set_on(self, _private: Private, mut mutator: Mut<'_, Repeated<$t>>) {
fn set_on<'b> (self, _private: Private, mut mutator: Mut<'b, Repeated<$t>>)
where
Repeated<$t>: 'b {
mutator.copy_from(self);
}
}

@ -185,7 +185,10 @@ impl<'msg> MutProxy<'msg> for BytesMut<'msg> {
}
impl SettableValue<[u8]> for &'_ [u8] {
fn set_on(self, _private: Private, mutator: BytesMut<'_>) {
fn set_on<'a>(self, _private: Private, mutator: Mut<'a, [u8]>)
where
[u8]: 'a,
{
// SAFETY: this is a `bytes` field with no restriction on UTF-8.
unsafe { mutator.inner.set(self) }
}
@ -695,7 +698,10 @@ impl<'msg> MutProxy<'msg> for ProtoStrMut<'msg> {
}
impl SettableValue<ProtoStr> for &'_ ProtoStr {
fn set_on(self, _private: Private, mutator: ProtoStrMut<'_>) {
fn set_on<'b>(self, _private: Private, mutator: Mut<'b, ProtoStr>)
where
ProtoStr: 'b,
{
// SAFETY: A `ProtoStr` has the same UTF-8 validity requirement as the runtime.
unsafe { mutator.bytes.inner.set(self.as_bytes()) }
}

@ -367,7 +367,9 @@ void GenerateRs(Context<Descriptor> msg) {
}
impl<'a> $pb$::SettableValue<$Msg$> for $Msg$View<'a> {
fn set_on(self, _private: $pb$::__internal::Private, _mutator: $pb$::Mut<$Msg$>) {
fn set_on<'b>(self, _private: $pb$::__internal::Private, _mutator: $pb$::Mut<'b, $Msg$>)
where
$Msg$: 'b {
todo!()
}
}

Loading…
Cancel
Save