pub struct Entity<T> { /* private fields */ }Expand description
A handle to state owned by the App.
An Entity<T> keeps its value alive while any handle to it exists. Read the value with
Entity::read, change it with Entity::update, and call Entity::downgrade for a
WeakEntity that doesn’t keep it alive.
Cloning is cheap. The clone is another handle to the same value, and two handles compare equal when they refer to the same entity.
When the last strong handle is dropped, GPUI releases the value at the end of the current update. See ownership and data flow for the model.
§Examples
struct Counter {
count: usize,
}
let counter: Entity<Counter> = cx.new(|_cx| Counter { count: 0 });
let same = counter.clone();
same.update(cx, |counter, _cx| counter.count += 1);
assert_eq!(counter.read(cx).count, 1);Implementations§
Source§impl<T: 'static> Entity<T>
impl<T: 'static> Entity<T>
Sourcepub fn entity_id(&self) -> EntityId
pub fn entity_id(&self) -> EntityId
Returns the id of the entity.
Two handles have the same id exactly when they refer to the same entity.
Sourcepub fn downgrade(&self) -> WeakEntity<T>
pub fn downgrade(&self) -> WeakEntity<T>
Returns a WeakEntity to the same value.
A weak handle doesn’t keep the value alive. Use it for back-references, and for callbacks that may run after the entity is released.
Sourcepub fn into_any(self) -> AnyEntity
pub fn into_any(self) -> AnyEntity
Converts this handle into an AnyEntity. The result keeps the value alive, without its type.
Sourcepub fn read<'a>(&self, cx: &'a App) -> &'a T
pub fn read<'a>(&self, cx: &'a App) -> &'a T
Returns a shared reference to the value, borrowed from the app.
The reference borrows cx, so you can’t update the app while you hold it. Copy out what you need,
or work inside a single Entity::update.
§Panics
Panics if this entity is currently being updated, for example when you read it from inside its own
Entity::update closure. Use the &mut T that the closure received instead.
§Examples
let name = cx.new(|_cx| String::from("GPUI"));
let len = name.read(cx).len();
name.update(cx, |name, _cx| name.push_str("!"));
assert_eq!(len, 4);
assert_eq!(name.read(cx), "GPUI!");Sourcepub fn read_with<R, C: AppContext>(
&self,
cx: &C,
f: impl FnOnce(&T, &App) -> R,
) -> R
pub fn read_with<R, C: AppContext>( &self, cx: &C, f: impl FnOnce(&T, &App) -> R, ) -> R
Calls read with the value and the app, and returns what read returns.
Use this when the callback needs the app as well as the value. It works with any AppContext,
including Context and AsyncApp. It panics in the same cases as Entity::read.
Sourcepub fn update<R, C: AppContext>(
&self,
cx: &mut C,
update: impl FnOnce(&mut T, &mut Context<'_, T>) -> R,
) -> R
pub fn update<R, C: AppContext>( &self, cx: &mut C, update: impl FnOnce(&mut T, &mut Context<'_, T>) -> R, ) -> R
Changes the value, and returns what update returns.
update receives a &mut T and a Context<T> for this entity. Use it for every change to
an entity. Call Context::notify inside the closure when anything that depends on the value has
changed. Effects queued by the closure, such as notifications and events, run when the outermost
update finishes.
Pass any AppContext as cx: &mut App, a Context of another entity, or an AsyncApp
inside a task.
§Panics
Panics if this entity is already being updated. Updating it from inside its own closure is the
usual cause; use the &mut T you were given instead. Updating other entities is fine.
§Examples
struct Counter {
count: usize,
}
let counter = cx.new(|_cx| Counter { count: 0 });
let count = counter.update(cx, |counter, cx| {
counter.count += 1;
cx.notify();
counter.count
});
assert_eq!(count, 1);Sourcepub fn as_mut<'a, C: AppContext>(&self, cx: &'a mut C) -> GpuiBorrow<'a, T>
pub fn as_mut<'a, C: AppContext>(&self, cx: &'a mut C) -> GpuiBorrow<'a, T>
Returns a guard that dereferences to the value, so you can change it with * or with methods.
The guard borrows cx for as long as it lives. When the guard is dropped, the entity is notified,
as if you had called Context::notify. Use Entity::update when the change needs a
Context.
Sourcepub fn write<C: AppContext>(&self, cx: &mut C, value: T)
pub fn write<C: AppContext>(&self, cx: &mut C, value: T)
Replaces the value with value, and notifies observers.
§Examples
let name = cx.new(|_cx| String::from("draft"));
name.write(cx, String::from("published"));
assert_eq!(name.read(cx), "published");Sourcepub fn update_in<R, C: VisualContext>(
&self,
cx: &mut C,
update: impl FnOnce(&mut T, &mut Window, &mut Context<'_, T>) -> R,
) -> C::Result<R>
pub fn update_in<R, C: VisualContext>( &self, cx: &mut C, update: impl FnOnce(&mut T, &mut Window, &mut Context<'_, T>) -> R, ) -> C::Result<R>
Like Entity::update, but also passes the Window that the entity is shown in.
Use it from a VisualContext, which is a context that has a window. Returns an error if the
window has been closed.
Source§impl<T: 'static> Entity<T>
impl<T: 'static> Entity<T>
Sourcepub fn next_event<Event>(
&self,
cx: &mut TestAppContext,
) -> impl Future<Output = Event>
pub fn next_event<Event>( &self, cx: &mut TestAppContext, ) -> impl Future<Output = Event>
Returns a future that resolves with the next event self emits.
The subscription is made when you call this, not when you await the future, so an event emitted in between is still received.
§Panics
Panics if the entity is released before it emits an event.
Source§impl<V: 'static> Entity<V>
impl<V: 'static> Entity<V>
Sourcepub fn next_notification(
&self,
advance_clock_by: Duration,
cx: &TestAppContext,
) -> impl Future<Output = ()>
pub fn next_notification( &self, advance_clock_by: Duration, cx: &TestAppContext, ) -> impl Future<Output = ()>
Returns a future that resolves on the entity’s next notification.
This advances the simulated clock by advance_clock_by as soon as you call it,
before the future is awaited. Use it when the notification comes from a timer
that the advance will fire.
§Panics
Panics if the entity is released while the future waits.
Source§impl<V> Entity<V>
impl<V> Entity<V>
Sourcepub fn condition<Evt>(
&self,
cx: &TestAppContext,
predicate: impl FnMut(&V, &App) -> bool,
) -> impl Future<Output = ()>where
Evt: 'static,
V: EventEmitter<Evt>,
pub fn condition<Evt>(
&self,
cx: &TestAppContext,
predicate: impl FnMut(&V, &App) -> bool,
) -> impl Future<Output = ()>where
Evt: 'static,
V: EventEmitter<Evt>,
Returns a future that resolves when predicate returns true for this entity.
The predicate is checked when the future is first polled, then again after each notification or event from the entity.
§Panics
Panics if the entity is released while the future waits.
Source§impl<T: Render> Entity<T>
impl<T: Render> Entity<T>
Sourcepub fn cached(self, style: StyleRefinement) -> ViewElement<Entity<T>>
pub fn cached(self, style: StyleRefinement) -> ViewElement<Entity<T>>
Returns this entity as an element laid out at a fixed root style.
An entity passed directly as a child already keeps its output while nothing it read has
changed, and sizes itself from its content. Use cached when the parent should decide the
view’s root style instead: the view is laid out at style, and its content is laid out
within the bounds that style produces. See
retained rendering.
Methods from Deref<Target = AnyEntity>§
Sourcepub fn entity_type(&self) -> TypeId
pub fn entity_type(&self) -> TypeId
Returns the TypeId of the type the entity was created with.
Sourcepub fn downgrade(&self) -> AnyWeakEntity
pub fn downgrade(&self) -> AnyWeakEntity
Returns a type-erased weak handle to the same entity. It doesn’t keep the entity alive.
Trait Implementations§
impl<T> Eq for Entity<T>
Source§impl<V: Focusable> Focusable for Entity<V>
impl<V: Focusable> Focusable for Entity<V>
Source§fn focus_handle(&self, cx: &App) -> FocusHandle
fn focus_handle(&self, cx: &App) -> FocusHandle
Source§impl<I: ImageCache> From<Entity<I>> for AnyImageCache
impl<I: ImageCache> From<Entity<I>> for AnyImageCache
Source§impl<T: ImageCache> ImageCacheProvider for Entity<T>
impl<T: ImageCache> ImageCacheProvider for Entity<T>
Source§fn provide(&mut self, _window: &mut Window, _cx: &mut App) -> AnyImageCache
fn provide(&mut self, _window: &mut Window, _cx: &mut App) -> AnyImageCache
image_cache element. Read moreSource§impl<V: 'static + Render> IntoElement for Entity<V>
impl<V: 'static + Render> IntoElement for Entity<V>
Source§type Element = ViewElement<Entity<V>>
type Element = ViewElement<Entity<V>>
Source§fn into_any_element(self) -> AnyElement
fn into_any_element(self) -> AnyElement
Source§impl<T: 'static> Ord for Entity<T>
impl<T: 'static> Ord for Entity<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T> PartialEq<Entity<T>> for WeakEntity<T>
impl<T> PartialEq<Entity<T>> for WeakEntity<T>
Source§impl<T> PartialEq<WeakEntity<T>> for Entity<T>
impl<T> PartialEq<WeakEntity<T>> for Entity<T>
Source§impl<T: 'static> PartialOrd for Entity<T>
impl<T: 'static> PartialOrd for Entity<T>
Source§impl<T: UniformListDecoration + 'static> UniformListDecoration for Entity<T>
impl<T: UniformListDecoration + 'static> UniformListDecoration for Entity<T>
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Entity<T>
impl<T> !UnwindSafe for Entity<T>
impl<T> Freeze for Entity<T>
impl<T> Send for Entity<T>
impl<T> Sync for Entity<T>
impl<T> Unpin for Entity<T>
impl<T> UnsafeUnpin for Entity<T>
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
Source§type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
Source§fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
Source§impl<E> AnimationExt for Ewhere
E: IntoElement + 'static,
impl<E> AnimationExt for Ewhere
E: IntoElement + 'static,
Source§fn with_animation(
self,
id: impl Into<ElementId>,
animation: Animation,
animator: impl Fn(Self, f32) -> Self + 'static,
) -> AnimationElement<Self>where
Self: Sized,
fn with_animation(
self,
id: impl Into<ElementId>,
animation: Animation,
animator: impl Fn(Self, f32) -> Self + 'static,
) -> AnimationElement<Self>where
Self: Sized,
Source§fn with_animations(
self,
id: impl Into<ElementId>,
animations: Vec<Animation>,
animator: impl Fn(Self, usize, f32) -> Self + 'static,
) -> AnimationElement<Self>where
Self: Sized,
fn with_animations(
self,
id: impl Into<ElementId>,
animations: Vec<Animation>,
animator: impl Fn(Self, usize, f32) -> Self + 'static,
) -> AnimationElement<Self>where
Self: Sized,
Source§fn with_spring<T>(
self,
id: impl Into<ElementId>,
animation: SpringAnimation<T>,
animator: impl FnOnce(Self, T::Output) -> Self + 'static,
) -> SpringAnimationElement<Self>
fn with_spring<T>( self, id: impl Into<ElementId>, animation: SpringAnimation<T>, animator: impl FnOnce(Self, T::Output) -> Self + 'static, ) -> SpringAnimationElement<Self>
animator. Read moreSource§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
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
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T, C> ComponentsInto<C> for T
impl<T, C> ComponentsInto<C> for T
Source§fn components_into(self) -> C
fn components_into(self) -> C
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FluentBuilder for Twhere
T: IntoElement,
impl<T> FluentBuilder for Twhere
T: IntoElement,
Source§fn map<U>(self, f: impl FnOnce(Self) -> U) -> Uwhere
Self: Sized,
fn map<U>(self, f: impl FnOnce(Self) -> U) -> Uwhere
Self: Sized,
self to f, and returns the result. Use it to continue a chain with a
closure.Source§fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Selfwhere
Self: Sized,
fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Selfwhere
Self: Sized,
then with self if condition is true. Otherwise returns self
unchanged.Source§fn when_else(
self,
condition: bool,
then: impl FnOnce(Self) -> Self,
else_fn: impl FnOnce(Self) -> Self,
) -> Selfwhere
Self: Sized,
fn when_else(
self,
condition: bool,
then: impl FnOnce(Self) -> Self,
else_fn: impl FnOnce(Self) -> Self,
) -> Selfwhere
Self: Sized,
then with self if condition is true, and else_fn with self
otherwise.Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.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, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
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> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<K> MapSeekTarget<K> for Kwhere
K: Ord,
impl<K> MapSeekTarget<K> for Kwhere
K: Ord,
fn cmp_cursor(&self, cursor_location: &K) -> Ordering
Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more