Skip to content

Add ScalarValue::downcast_type() method for GraphQLScalar parsing optimization (#819) #1329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

tyranron
Copy link
Member

Part of #819
Continued from #1324

Synopsis

As a counterpart for the ScalarValue::from_displayable() allowing optimizing ScalarValue creation from arbitrary types, this PR introduces a ScalarValue::downcast_type() method, which allows checking whether a generic ScalarValue already represents the desired type:

#[derive(GraphQLScalar)]
#[graphql(from_input_with = Self::from_input, transparent)]
struct Name(ArcStr);

impl Name {
    fn from_input<S: ScalarValue>(
        v: &Scalar<S>,
    ) -> Result<Self, <S as TryScalarValueTo<'_, &str>>::Error> {
        // Check if our `ScalarValue` is represented by an `ArcStr` already, and if so, 
        // do the cheap `Clone` instead of allocating a new `ArcStr` in its `From<&str>` 
        // implementation.
        if let Some(s) = v.downcast_type::<ArcStr>() {
             Ok(Self(s.clone()))
        } else {
            v.try_to::<&str>().map(|s| Self(s.into()))
        }
    }
}

@tyranron tyranron added this to the 0.17.0 milestone Jun 16, 2025
@tyranron tyranron self-assigned this Jun 16, 2025
@tyranron tyranron added enhancement Improvement of existing features or bugfix k::api Related to API (application interface) k::performance Related to performance feature New feature or request labels Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of existing features or bugfix feature New feature or request k::api Related to API (application interface) k::performance Related to performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant