Alyssa Haroldsen | d5d207f | 2023-08-24 13:39:23 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2023 Google LLC. All rights reserved. |
Alyssa Haroldsen | d5d207f | 2023-08-24 13:39:23 -0700 | [diff] [blame] | 3 | // |
Joshua Haberman | 4a51303 | 2023-09-08 17:12:50 -0700 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
Alyssa Haroldsen | d5d207f | 2023-08-24 13:39:23 -0700 | [diff] [blame] | 7 | |
| 8 | //! Runtime-internal macros |
| 9 | |
Alyssa Haroldsen | d5d207f | 2023-08-24 13:39:23 -0700 | [diff] [blame] | 10 | /// Defines a `impl SettableValue<$proxied> for SomeType` body that forwards to |
| 11 | /// another implementation. |
| 12 | /// |
| 13 | /// # Example |
| 14 | /// ```ignore |
| 15 | /// impl<'a, const N: usize> SettableValue<[u8]> for &'a [u8; N] { |
| 16 | /// // Use the `SettableValue<[u8]>` implementation for `&[u8]`: |
| 17 | /// impl_forwarding_settable_value!([u8], self => &self[..]); |
| 18 | /// } |
| 19 | /// ``` |
| 20 | macro_rules! impl_forwarding_settable_value { |
| 21 | ($proxied:ty, $self:ident => $self_forwarding_expr:expr) => { |
Jakob Buchgraber | ab11a0d | 2023-11-27 08:14:21 -0800 | [diff] [blame] | 22 | fn set_on<'b>( |
Alyssa Haroldsen | d5d207f | 2023-08-24 13:39:23 -0700 | [diff] [blame] | 23 | $self, |
| 24 | _private: $crate::__internal::Private, |
Jakob Buchgraber | ab11a0d | 2023-11-27 08:14:21 -0800 | [diff] [blame] | 25 | mutator: $crate::Mut<'b, $proxied>, |
| 26 | ) where $proxied: 'b { |
Alyssa Haroldsen | d5d207f | 2023-08-24 13:39:23 -0700 | [diff] [blame] | 27 | ($self_forwarding_expr).set_on(Private, mutator) |
| 28 | } |
| 29 | |
| 30 | fn set_on_absent( |
| 31 | $self, |
| 32 | _private: $crate::__internal::Private, |
| 33 | absent_mutator: <$proxied as $crate::ProxiedWithPresence>::AbsentMutData<'_>, |
| 34 | ) -> <$proxied as $crate::ProxiedWithPresence>::PresentMutData<'_> { |
| 35 | ($self_forwarding_expr).set_on_absent($crate::__internal::Private, absent_mutator) |
| 36 | } |
| 37 | |
| 38 | fn set_on_present( |
| 39 | $self, |
| 40 | _private: $crate::__internal::Private, |
| 41 | present_mutator: <$proxied as $crate::ProxiedWithPresence>::PresentMutData<'_>, |
| 42 | ) { |
| 43 | ($self_forwarding_expr).set_on_present($crate::__internal::Private, present_mutator) |
| 44 | } |
| 45 | }; |
| 46 | } |
| 47 | pub(crate) use impl_forwarding_settable_value; |