Skip to content

Make Resource Ready/Pending State Explicit #5105

@mcmah309

Description

@mcmah309

Feature Request

Background

It would be great if the explicitness of Resource<T> ready/pending was built in to the type.

Locally I use Pending as a type alias for Option when the resource has not finished yet

/// A type alias for the state of a resource
pub type Pending<T> = Option<T>;

I think it makes things more clear. Since

let resource: Resource<Option<i32>> = use_resource(move |_| async move {
    todo!()
});

// rather than
let read: GenerationalRef<Ref<'_, Option<Option<i32>>>> = resource.read();
// It becomes
let read: GenerationalRef<Ref<'_, Pending<Option<i32>>>> = resource.read();
match &*read {
   Pending::Some(_) => todo!(),
   Pending::None => todo!(),
}

But from an IDE perspective, this currently does not make code actions more clear, since the rust-analyzer looks through type aliases, which is a known issue - rust-lang/rust-analyzer#1666

Solution

Instead of using Option or a type alias here, we could do better with something like Poll. That would it would look like

let read: GenerationalRef<Ref<'_, Poll<Option<i32>>>> = resource.read();
match &*read {
   Poll::Ready(_) => todo!(),
   Poll::Pending => todo!(),
}

We would probably want to use our own type rather than the std libs Poll though. If we choose a different name, we should avoid a name like ResourceValue or ResourceState, so we can use it more generally.

I believe an endless while loop on read will never yield a different value?

while resource.read().is_none() { } // If this is true once is it always true?

If so, Poll is definitely not the right name, since that is not the behavior. Something like AsyncValue may be more appropriate. But whatever the name is, the variants Ready/Pending are probably correct choice.

Related

Likely related to #4846

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions