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 extension() /
set_extension() / clear_extension() will panic.
Field number 0 is invalid in protobuf. Codegen never emits it;
a descriptor constructed with 0 will never match valid wire data.
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())).