pub struct Field<T> { /* private fields */ }Expand description
A Request Context field holding a T, which travels as JSON.
Declare it as a module-level static: static USER: Field<User> = Field::new("user");. Its type argument is the value’s type, which Encore
describes, and checks the bindings of the field by name against. It is
serialized with serde, and must be a type Encore can describe as a config’s
type.
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 field. const so it can initialize a module-level
static.
The name is unique within the app, in kebab-case: lowercase letters, digits and hyphens, starting with a letter and ending with a letter or digit. Encore’s parser checks it.
Sourcepub const fn named(name: &'static str) -> Self
pub const fn named(name: &'static str) -> Self
Binds a field declared elsewhere in the app, possibly in another language. Encore checks the value’s type against the declaration’s. It travels as the declaration says: carried on Pub/Sub messages, and put on spans, as declared.
Sourcepub const fn pubsub(self) -> Self
pub const fn pubsub(self) -> Self
Also carries the field on the Pub/Sub messages published with it set, into the context of the handlers that receive them. Its value may then encode to at most 1 KiB, GCP Pub/Sub’s limit on an attribute’s value.
Part of the declaration: it takes the field by value, so it cannot be
called on the static.
Sourcepub const fn trace(self) -> Self
pub const fn trace(self) -> Self
Puts the value on the spans started with it set, as the attribute
encore.ctx.<name>, holding its JSON: for identifiers worth finding
traces by, such as a tenant’s. Never for 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
Tags the field with key and value: key/value classification, such
as .tag("data", "pci"), for policies to read. One call per tag, each
with its own key. Encore reads them from the source, so both are string
literals. A key is lowercase letters, digits, _ and -, starting with
a letter and not with encore, which is kept for Encore’s own tags; a
value is the same characters, or empty. Each is at most 63 characters,
and a field has at most 32 tags.
Part of the declaration: it takes the field by value, so it cannot be
called on the static.
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>
The field’s value in the current context; None when it is not set.
Each call decodes the value anew. A value that does not decode as a
T, as one set by a service that disagrees about its type, reads as
not set; Encore logs it, once.
Sourcepub fn get_in(&self, cx: &Context) -> Option<T>
pub fn get_in(&self, cx: &Context) -> Option<T>
The field’s value in cx; None when it is not set.
Sourcepub fn context(&self, value: &T) -> Result<Context, Error>
pub fn context(&self, value: &T) -> Result<Context, Error>
The current context with the field set to value, for code that
passes contexts explicitly.
§Errors
Error::Encode when the value does not serialize, and
Error::Size when it encodes to more than the field may: 4 KiB, or
1 KiB for a field declared with pubsub, or makes the
context’s fields encode to more than 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>
cx with the field set to value. See context.
Sourcepub fn without_in(&self, cx: &Context) -> Context
pub fn without_in(&self, cx: &Context) -> Context
cx without the field.
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>
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