pub struct Reflector { /* private fields */ }Expand description
Descriptor index serving gRPC server reflection queries.
Build one from the wire bytes of a FileDescriptorSet (typically
embedded with include_bytes! from
connectrpc_build::Config::emit_descriptor_set output) or from an
existing DescriptorPool, and hand it to
ReflectionService.
use connectrpc_reflection::Reflector;
// In real code: include_bytes!(concat!(env!("OUT_DIR"), "/app.fds.bin"))
let reflector = Reflector::from_descriptor_set_bytes(descriptor_set_bytes()).unwrap();§Multiple descriptor sets
add_descriptor_set_bytes merges
further sets into the index. Files whose name is already registered
are skipped (first registration wins), so sets that each carry their
own copy of shared imports — google/protobuf/*.proto, common
vendored protos — merge cleanly.
Implementations§
Source§impl Reflector
impl Reflector
Sourcepub fn from_descriptor_set_bytes(bytes: &[u8]) -> Result<Self, ReflectionError>
pub fn from_descriptor_set_bytes(bytes: &[u8]) -> Result<Self, ReflectionError>
Build a reflector from wire-format FileDescriptorSet bytes.
The set should carry the transitive import closure of the files
it contains (both protoc --include_imports and
Config::emit_descriptor_set guarantee this); imports missing
from the set are silently omitted from file-closure responses.
§Errors
Returns ReflectionError when the bytes do not decode as a
FileDescriptorSet, the descriptors do not link, or a contained
file has no name.
Sourcepub fn from_descriptor_pool(
pool: Arc<DescriptorPool>,
) -> Result<Self, ReflectionError>
pub fn from_descriptor_pool( pool: Arc<DescriptorPool>, ) -> Result<Self, ReflectionError>
Serve reflection from an existing DescriptorPool — typically
the lazily-built descriptor_pool() that a buffa-generated package
exposes when reflection codegen is enabled, which spares the build
script a separate emit_descriptor_set step.
The pool must cover every proto you want resolvable. Under
buf generate’s default per-directory plugin strategy, each
generated package embeds only its own package’s closure — set
strategy: all on the buffa plugin so any one package’s pool
spans the whole codegen run.
Response payloads are re-encoded from the pool’s parsed
FileDescriptorProtos. buffa preserves unknown fields, so the
bytes are semantically faithful to the compiler’s output but not
guaranteed byte-identical (field ordering is canonicalized). For
byte-exact responses, build from
from_descriptor_set_bytes.
A reflector adopting a pool that other references point at — a
generated package’s pool always qualifies, since the lazy static
keeps one — cannot be extended with
add_descriptor_set_bytes;
build from bytes if you need to merge sets.
§Errors
Returns ReflectionError::UnnamedFile when a pool file has no
name.
Sourcepub fn add_descriptor_set_bytes(
&mut self,
bytes: &[u8],
) -> Result<(), ReflectionError>
pub fn add_descriptor_set_bytes( &mut self, bytes: &[u8], ) -> Result<(), ReflectionError>
Merge another wire-format FileDescriptorSet into the index.
Files whose name is already registered are skipped, so shared imports duplicated across sets do not conflict.
§Errors
Returns ReflectionError when the bytes do not decode or link,
a contained file has no name, or any other reference to the
backing pool exists (ReflectionError::SharedPool) — which is
always the case for reflectors built with
from_descriptor_pool from a
long-lived pool. On error the reflector should be discarded: the
pool may have absorbed part of the failed set.
Sourcepub fn with_services<I, S>(self, names: I) -> Self
pub fn with_services<I, S>(self, names: I) -> Self
Restrict the service list advertised by ListServices to the
given fully-qualified names, in the given order.
Like the Go grpcreflect Namer, this affects only
ListServices; files and symbols in the descriptor set stay
resolvable. Use it when the set’s import closure carries services
you do not actually mount. Names absent from the descriptor set
are advertised as given — the protocol does not require the list
to be resolvable.
Calling this more than once replaces the previous list; it
does not accumulate (unlike tonic’s with_service_name).
Sourcepub fn service_names(&self) -> Vec<String>
pub fn service_names(&self) -> Vec<String>
The fully-qualified service names ListServices will advertise:
every service in the descriptor pool in registration order plus
the reflection services themselves (matching grpc-go, which
always lists them) — or, verbatim, the override installed by
with_services.
Sourcepub fn pool(&self) -> &DescriptorPool
pub fn pool(&self) -> &DescriptorPool
The descriptor pool backing this reflector, for read-only inspection (listing files, resolving descriptors).