pub struct Extension<C: ExtensionCodec> { /* private fields */ }Expand description
Typed extension descriptor.
Emitted by codegen as a pub const for each extend declaration. The
type parameter C is an ExtensionCodec — a zero-sized codec struct
that encodes the proto field type. Users don’t name C directly; it flows
through inference from the const to the ExtensionSet method call.
For proto2 extensions declared with [default = ...], codegen stores a
function pointer that lazily produces the default value. See
Extension::with_default and ExtensionSet::extension_or_default.
Implementations§
Source§impl<C: ExtensionCodec> Extension<C>
impl<C: ExtensionCodec> Extension<C>
Sourcepub const fn new(number: u32, extendee: &'static str) -> Self
pub const fn new(number: u32, extendee: &'static str) -> Self
Construct an extension descriptor for the given field number and extendee message.
const fn so that generated pub const items can use it.
extendee is the fully-qualified proto type name (no leading dot) of
the message this extension extends — e.g. "google.protobuf.FieldOptions".
Passing an extension with a mismatched extendee to an accessor that
reads, writes or clears it panics; the
module documentation lists those accessors.
§Panics
Panics if number is 0 or greater than i32::MAX. In a const item
the panic is a compile error.
The upper bound is i32::MAX because an extension of a message with
option message_set_wire_format = true can have a number up to that.
For any other extendee the largest valid number is
MAX_FIELD_NUMBER: with a larger
one, set_extension() stores a field that encodes with a tag decoders
reject.
§Examples
use buffa::extension::codecs::Int32;
use buffa::Extension;
const WEIGHT: Extension<Int32> = Extension::new(50001, "my.pkg.Options");
assert_eq!(WEIGHT.number(), 50001);A const with the number 0 does not compile:
use buffa::extension::codecs::Int32;
use buffa::Extension;
const ZERO: Extension<Int32> = Extension::new(0, "my.pkg.Options");
assert_eq!(ZERO.number(), 0);Sourcepub const fn with_default(
number: u32,
extendee: &'static str,
default: fn() -> C::Value,
) -> Self
pub const fn with_default( number: u32, extendee: &'static str, default: fn() -> C::Value, ) -> Self
Construct an extension descriptor with a proto2 [default = ...] value.
The default is returned by ExtensionSet::extension_or_default when
the extension is absent. ExtensionSet::extension continues to return
None when absent — this is additive, not a semantic change.
The function pointer is called lazily on each extension_or_default
call. For Copy scalars codegen emits a const fn; for String and
bytes a regular fn (allocates on each call — same cost as a
hand-written .unwrap_or_else(|| "x".into())).
§Panics
Panics if number is 0 or greater than i32::MAX, as
new does.