Repository navigation
Borrow a &str implicit argument without alloc - #288
Merged
Merged
Conversation
A `#[cgp_fn]` or `#[cgp_impl]` with an `#[implicit] name: &str` argument required a `String` field, so it failed in a crate without an allocator with "cannot find type `String`". The argument now requires `Value: AsRef<str> + 'static` and reads the field with `.as_ref()`, so a `String`, a `Cow<str>`, or a `Box<str>` field works and none needs an allocator. An `Option<&str>` argument required an `Option<String>` field for the same reason. It now requires `Value: OptionAsRef<str>`, a new helper trait in `cgp-field` whose blanket impl covers any `Option<U>` with `U: AsRef<T>`. A getter keeps the `String` field, because its `HasField` bound is also used by the `WithProvider` adapter, which expects the field type itself. Every conversion and field read the macros emit is now a fully qualified trait call, and the bounds name `::core::convert::AsRef` and `::core::convert::AsMut`, so a local trait of the same name cannot capture them.
soareschen
force-pushed
the
docs/no-std-implicits
branch
from
November 7, 2026 22:50
d827774 to
45059ed
Compare
&str implicit argument without alloc
soareschen
marked this pull request as ready for review
November 7, 2026 22:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
[deepseek-v4.1-flash] A
#[cgp_fn]or#[cgp_impl]with an#[implicit] name: &strargument now reads anyAsRef<str>field, and anOption<&str>argument reads anyOption<U>whoseUconverts to&str, so neither needs an allocator. Every conversion and field read the macros emit is also a fully qualified trait call.Motivation
The
&strimplicit argument required aStringfield, so a crate without an allocator failed with "cannot find typeString". TheOption<&str>form required anOption<String>field for the same reason. Both bounds tied the field to one concrete type, and the first one made the argument unusable in theno_stdbuild that the previous change added.Impact
The generated bound for
&strchanges fromValue = StringtoValue: AsRef<str> + 'static, and the read from.as_str()to.as_ref(). AStringfield still satisfies it, so existing contexts keep compiling, and aCow<str>or aBox<str>field now works too.The generated bound for
Option<&str>changes fromValue = Option<String>toValue: OptionAsRef<str> + 'static, and the read from.as_deref()to.option_as_ref().OptionAsRef<T>is a new trait incgp-field, with a blanket impl for everyOption<U>whoseUimplementsAsRef<T>; its?Sizedbound is what letsTbestr.A getter keeps the
Stringfield, because itsHasFieldbound is also used by theWithProvideradapter, which expects the field type itself. ApplyingAsRef<str>there brokechain_deeply_nestedwith aValue == strmismatch.Every conversion and field read the macros emit is now a fully qualified trait call —
::core::convert::AsRef::as_ref,::core::convert::AsMut::as_mut,::core::clone::Clone::clone,::core::option::Option::as_ref/as_mut/as_deref_mut,::cgp::macro_prelude::OptionAsRef::option_as_ref, and::cgp::macro_prelude::HasField::get_field— and the bounds name::core::convert::AsRefand::core::convert::AsMut, so a local trait of the same name cannot capture them. A probe crate that defines its ownHasFieldandAsReftraits compiles, which it did not before.Three tests cover
#[cgp_impl]with a&strand anOption<&str>implicit, and run the provider against aString, a&str, and aCow<str>field.cgp-no-std-testsuses both implicit forms and still builds forx86_64-unknown-none.