pub struct Field<T> { /* private fields */ }Expand description
A typed Request Context field serialized as JSON.
Declare as a module-level static (for example,
static USER: Field<User> = Field::new("user");). The type parameter T
defines the field’s schema, which Encore validates across all bindings by
name. T must be serializable with Serde and representable in Encore’s
config schema.
Implementations§
Source§impl<T> Field<T>
impl<T> Field<T>
Sourcepub const fn new(name: &'static str) -> Self
pub const fn new(name: &'static str) -> Self
Declares a Request Context field.
This constructor is const so it can initialize a module-level
static. name must be unique across the app and use kebab-case
(lowercase letters, digits, and hyphens, starting with a letter and
ending with a letter or digit).
Sourcepub const fn named(name: &'static str) -> Self
pub const fn named(name: &'static str) -> Self
Binds a field declared elsewhere in the application (possibly in another service or language).
Encore checks that T is compatible with the original declaration, and
the bound field inherits its Pub/Sub and tracing behavior.
Sourcepub const fn pubsub(self) -> Self
pub const fn pubsub(self) -> Self
Propagates this field on Pub/Sub messages published while it is set, making it available in subscriber contexts.
Enabling Pub/Sub propagation limits the field’s encoded size to 1 KiB (matching GCP Pub/Sub’s attribute value limit).
Sourcepub const fn trace(self) -> Self
pub const fn trace(self) -> Self
Records this field’s JSON value as the encore.ctx.<name> attribute on
spans started while it is set.
Useful for searchable identifiers such as tenant IDs. Never enable on fields containing secrets.
Sourcepub const fn tag(self, _key: &'static str, _value: &'static str) -> Self
pub const fn tag(self, _key: &'static str, _value: &'static str) -> Self
Attaches a key/value tag to the field (for example, .tag("data", "pci"))
for policy and governance tooling.
Call once per tag with unique keys. Both key and value must be string
literals so Encore can parse them statically. Keys must start with a
letter, contain only lowercase letters, digits, _, and -, and must
not start with encore (reserved for Encore’s own tags). Values use the
same character set or may be empty. Each key and value is at most 63
characters, and a field may have at most 32 tags.
use encore::reqctx::Field;
static TENANT: Field<String> = Field::new("tenant").tag("data", "pii");Source§impl<T: Serialize + DeserializeOwned> Field<T>
impl<T: Serialize + DeserializeOwned> Field<T>
Sourcepub fn get(&self) -> Option<T>
pub fn get(&self) -> Option<T>
Returns this field’s value from the current context, or None if unset.
Each call decodes the value from the context. If the stored payload
cannot be decoded as T (for example, due to a type mismatch across
services), Encore logs a warning once and returns None.
Sourcepub fn get_in(&self, cx: &Context) -> Option<T>
pub fn get_in(&self, cx: &Context) -> Option<T>
Returns this field’s value from cx, or None if unset.
Sourcepub fn context(&self, value: &T) -> Result<Context, Error>
pub fn context(&self, value: &T) -> Result<Context, Error>
Returns a copy of the current context with this field set to value.
§Errors
Returns Error::Encode if value fails to serialize, or
Error::Size if the encoded value exceeds the field limit (4 KiB, or
1 KiB with Field::pubsub) or causes the total Request Context to
exceed 8 KiB.
Sourcepub fn context_in(&self, cx: &Context, value: &T) -> Result<Context, Error>
pub fn context_in(&self, cx: &Context, value: &T) -> Result<Context, Error>
Returns a copy of cx with this field set to value. See
context.
Sourcepub fn without(&self) -> Context
pub fn without(&self) -> Context
Returns a copy of the current context with this field removed.
Sourcepub fn without_in(&self, cx: &Context) -> Context
pub fn without_in(&self, cx: &Context) -> Context
Returns a copy of cx with this field removed.
Trait Implementations§
Auto Trait Implementations§
impl<T> Forget for Field<T>
impl<T> Freeze for Field<T>
impl<T> Move for Field<T>
impl<T> RefUnwindSafe for Field<T>
impl<T> Send for Field<T>
impl<T> Sync for Field<T>
impl<T> Unpin for Field<T>
impl<T> UnsafeUnpin for Field<T>
impl<T> UnwindSafe for Field<T>where
&'static str: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request