pub struct MappedQueryResource<T, U, E> { /* private fields */ }core only.Expand description
A mapped view over a QueryResource that applies a SelectTransform.
This implements the “select” pattern: multiple consumers can derive
different views from the same underlying cached data, each with their own
MappedQueryResource holding a different transform function. The source
data is shared, so there is no duplication.
§Type parameters
T: The source data type (the cached query result).U: The projected output type (the derived view).E: The error type (carried through for API consistency).
§Storage
Source data is held as Option<Arc<T>> (audit #20) so that cloning a
MappedQueryResource (e.g. for derived views) is a cheap Arc::clone
rather than a full copy of T. Arc<T> is Send + Sync exactly when T
is, so the existing bounds are preserved.
Implementations§
Source§impl<T, U, E> MappedQueryResource<T, U, E>
impl<T, U, E> MappedQueryResource<T, U, E>
Sourcepub fn new(
source_data: Option<Arc<T>>,
transform: SelectTransform<T, U>,
) -> Self
pub fn new( source_data: Option<Arc<T>>, transform: SelectTransform<T, U>, ) -> Self
Create a new mapped resource.
Takes ownership of the source data as Option<Arc<T>> (audit #20). For
the common case of constructing from a plain T, wrap it with
Some(Arc::new(t)).
Sourcepub fn data(&self) -> Option<U>
pub fn data(&self) -> Option<U>
Apply the transform to get the selected data.
Note (audit #3): This re-applies the transform closure on every call.
MappedQueryResource is a derived view with no separate output cache — it
stores only the source data and the transform function. If the transform is
expensive and you need the result multiple times in a single render pass
(e.g., once for display and once for an equality check), cache the result
in a local variable:
use gpui_query::core::{MappedQueryResource, SelectTransform};
let transform = SelectTransform::new(|v: &Vec<i32>| v.len());
let mapped = MappedQueryResource::<_, usize, ()>::new(Some(std::sync::Arc::new(vec![1, 2, 3])), transform);
let data = mapped.data(); // transform runs once
assert_eq!(data, Some(3));
// use `data` freely belowFor lightweight transforms (field access, counting, simple projections) the cost is negligible and no caching is needed.
Sourcepub fn source_data(&self) -> Option<&T>
pub fn source_data(&self) -> Option<&T>
Read-only access to the source data.
Returns Option<&T> by dereferencing the stored Arc<T> (audit #20).
Keeping the &T return type (rather than &Arc<T>) is the least
disruptive choice: existing callers compare the pointed-to T and do
not need to change. Used by the hook layer to detect when the source
has changed before re-storing.
Sourcepub fn source_arc(&self) -> Option<Arc<T>>
pub fn source_arc(&self) -> Option<Arc<T>>
Cheaply hand out the cached source Arc<T> as an owned value.
Returns Option<Arc<T>> via a refcount bump (Arc::clone) — no T
clone. Audit H1: the hook layer uses this to compare the cached source
against a fresh read WITHOUT cloning T on unchanged notifications.
Because the returned Arc<T> is owned, the mapped borrow ends with
this call, so a subsequent entity.read_with does not create the
nested borrow that audit #115 removed.
Sourcepub fn update_source(&mut self, data: Option<Arc<T>>)
pub fn update_source(&mut self, data: Option<Arc<T>>)
Update the source data from the underlying query resource.
Call this when the source QueryResource changes (fetch completes,
cache invalidation, etc.) to keep the mapped view in sync. The transform
is not applied here — it is applied lazily when data()
is called. Takes Option<Arc<T>> so callers can hand over a cheap
Arc::clone instead of cloning the full T (audit #20).
Trait Implementations§
Source§impl<T: Clone, U: Clone, E: Clone> Clone for MappedQueryResource<T, U, E>
impl<T: Clone, U: Clone, E: Clone> Clone for MappedQueryResource<T, U, E>
Source§fn clone(&self) -> MappedQueryResource<T, U, E>
fn clone(&self) -> MappedQueryResource<T, U, E>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<T: Eq, U: Eq, E: Eq> Eq for MappedQueryResource<T, U, E>
impl<T: PartialEq, U: PartialEq, E: PartialEq> StructuralPartialEq for MappedQueryResource<T, U, E>
Auto Trait Implementations§
impl<T, U, E> !RefUnwindSafe for MappedQueryResource<T, U, E>
impl<T, U, E> !UnwindSafe for MappedQueryResource<T, U, E>
impl<T, U, E> Freeze for MappedQueryResource<T, U, E>
impl<T, U, E> Send for MappedQueryResource<T, U, E>
impl<T, U, E> Sync for MappedQueryResource<T, U, E>
impl<T, U, E> Unpin for MappedQueryResource<T, U, E>
impl<T, U, E> UnsafeUnpin for MappedQueryResource<T, U, E>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
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<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§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 more