protobuf/
ext.rs

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
7use std::marker::PhantomData;
8
9use crate::message::Message;
10use crate::types::ProtobufType;
11
12/// Optional ext field
13pub 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
22pub 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
30impl<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
41impl<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}