Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 1 | //! Utilities to support "extension" fields. |
| 2 | //! |
| 3 | //! Extensions are [described in the official protobuf documentation][exts]. |
| 4 | //! |
| 5 | //! [exts]: https://developers.google.com/protocol-buffers/docs/proto#extensions |
| 6 | |
| 7 | use std::marker::PhantomData; |
| 8 | |
Haibo Huang | d32e6ee | 2020-08-12 13:52:04 -0700 | [diff] [blame^] | 9 | use crate::message::Message; |
| 10 | use crate::types::ProtobufType; |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 11 | |
| 12 | /// Optional ext field |
| 13 | pub struct ExtFieldOptional<M: Message, T: ProtobufType> { |
| 14 | /// Extension field number |
| 15 | pub field_number: u32, |
| 16 | /// Marker |
| 17 | // TODO: hide |
| 18 | pub phantom: PhantomData<(M, T)>, |
| 19 | } |
| 20 | |
| 21 | /// Repeated ext field |
| 22 | pub struct ExtFieldRepeated<M: Message, T: ProtobufType> { |
| 23 | /// Extension field number |
| 24 | pub field_number: u32, |
| 25 | /// Extension field number |
| 26 | // TODO: hide |
| 27 | pub phantom: PhantomData<(M, T)>, |
| 28 | } |
| 29 | |
| 30 | impl<M: Message, T: ProtobufType> ExtFieldOptional<M, T> { |
| 31 | /// Get a copy of value from a message. |
| 32 | /// |
| 33 | /// Extension data is stored in [`UnknownFields`](crate::UnknownFields). |
| 34 | pub fn get(&self, m: &M) -> Option<T::Value> { |
| 35 | m.get_unknown_fields() |
| 36 | .get(self.field_number) |
| 37 | .and_then(T::get_from_unknown) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl<M: Message, T: ProtobufType> ExtFieldRepeated<M, T> { |
| 42 | /// Get a copy of value from a message (**not implemented**). |
| 43 | pub fn get(&self, _m: &M) -> Vec<T::Value> { |
| 44 | // TODO |
| 45 | unimplemented!() |
| 46 | } |
| 47 | } |