Skip to content

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
@tyranron tyranron marked this pull request as ready for review June 18, 2025 14:23
@tyranron tyranron enabled auto-merge (squash) June 18, 2025 14:33
@tyranron tyranron merged commit d3e1697 into master Jun 18, 2025
179 of 180 checks passed
@tyranron tyranron deleted the downcast-scalar-value branch June 18, 2025 14:50
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.

2 participants