pub struct FieldContext { /* private fields */ }Expand description
Type-erased context for one field’s binding, metadata, and focus request slot.
The context itself is intentionally not generic. This lets use_binding distinguish an
absent context from a present context containing the wrong value type, and lets Field
accept any value type without becoming generic itself.
§Equality
Two contexts are equal when their bindings are equal under Binding’s identity equality and
their metadata is equal, regardless of when either context was constructed. The focus request
slot is intentionally excluded: Field pins the slot of the first context it receives for
its lifetime, so the slot carried by a context built on a later render is never observed by
descendants, and comparing it would only defeat memoization.
Implementations§
Source§impl FieldContext
impl FieldContext
Sourcepub fn with_binding<T: 'static>(self, binding: Binding<T>) -> Self
pub fn with_binding<T: 'static>(self, binding: Binding<T>) -> Self
Replaces the context’s value binding.
Sourcepub fn with_meta_values(self, values: FieldMetaValues) -> Self
pub fn with_meta_values(self, values: FieldMetaValues) -> Self
Adds producer values that Field realizes as signal-backed metadata.
Sourcepub fn focus_request(&self) -> FocusRequest
pub fn focus_request(&self) -> FocusRequest
Returns the context’s focus request slot.
Field pins the slot of the first context it receives, so producers that request focus
through a context must keep that context stable across renders.
Sourcepub fn resolve<T: 'static>(&self) -> Binding<T>
pub fn resolve<T: 'static>(&self) -> Binding<T>
Resolves the context binding for T.
§Panics
Panics when the field context contains no binding or a binding for a different value type.
Sourcepub fn try_resolve<T: 'static>(
&self,
) -> Result<Option<Binding<T>>, BindingTypeMismatch>
pub fn try_resolve<T: 'static>( &self, ) -> Result<Option<Binding<T>>, BindingTypeMismatch>
Tries to resolve the context binding for T without panicking.
Ok(Some(_)) contains a matching binding, Ok(None) means the context has no value
binding, and Err(_) reports the actual and requested value types when they differ. This
lets a control support more than one binding type while preserving absence as the signal to
use standalone state.
§Errors
Returns BindingTypeMismatch when the context contains a binding whose value type is not
T.
use dioxus_field::{Binding, BindingTypeMismatch, FieldContext};
#[derive(Clone, Copy)]
enum CheckboxState {
Checked,
Indeterminate,
Unchecked,
}
enum CheckboxBinding {
State(Binding<CheckboxState>),
Boolean(Binding<bool>),
}
fn resolve_checkbox_binding(
context: &FieldContext,
) -> Result<Option<CheckboxBinding>, BindingTypeMismatch> {
match context.try_resolve::<CheckboxState>() {
Ok(Some(binding)) => Ok(Some(CheckboxBinding::State(binding))),
Ok(None) => Ok(None),
Err(_) => context
.try_resolve::<bool>()
.map(|binding| binding.map(CheckboxBinding::Boolean)),
}
}Trait Implementations§
Source§impl Clone for FieldContext
impl Clone for FieldContext
Source§fn clone(&self) -> FieldContext
fn clone(&self) -> FieldContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more