rust/build_error.rs | 7 +++ rust/kernel/configfs.rs | 2 +- rust/kernel/const_eval.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++ rust/kernel/device_id.rs | 4 ++ rust/kernel/drm/device.rs | 4 +- rust/kernel/drm/ioctl.rs | 4 +- rust/kernel/kunit.rs | 6 +-- rust/kernel/lib.rs | 1 + rust/kernel/miscdevice.rs | 2 +- rust/kernel/net/phy.rs | 2 +- rust/kernel/prelude.rs | 1 + rust/kernel/str.rs | 15 +++--- rust/macros/const_eval.rs | 51 ++++++++++++++++++++ rust/macros/lib.rs | 39 ++++++++++++++++ rust/macros/module.rs | 4 +- 15 files changed, 237 insertions(+), 20 deletions(-)
Add a mechanism to perform const trait method calls, with the same type
inference capabilities. This would only work for concrete types (not inside
generic functions).
As an example, convert `as_char_ptr_in_const_context`, so instead of
kernel::str::as_char_ptr_in_const_context(c_str)
one may write
const_call!(c_str.as_char_ptr())
instead.
Signed-off-by: Gary Guo <gary@garyguo.net>
---
Changes in v2:
- Split from `cv!()` alt series, and become a general infra instead.
- Link to v1: https://patch.msgid.link/20260828-cv-v1-0-694a695ff17f@garyguo.net
---
Gary Guo (3):
rust: const_eval: add `#[const_eval_only]` attribute
rust: const_eval: allow const trait method invocation in some contexts
rust: str: convert `as_char_ptr` to work with `const_call!`
rust/build_error.rs | 7 +++
rust/kernel/configfs.rs | 2 +-
rust/kernel/const_eval.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++
rust/kernel/device_id.rs | 4 ++
rust/kernel/drm/device.rs | 4 +-
rust/kernel/drm/ioctl.rs | 4 +-
rust/kernel/kunit.rs | 6 +--
rust/kernel/lib.rs | 1 +
rust/kernel/miscdevice.rs | 2 +-
rust/kernel/net/phy.rs | 2 +-
rust/kernel/prelude.rs | 1 +
rust/kernel/str.rs | 15 +++---
rust/macros/const_eval.rs | 51 ++++++++++++++++++++
rust/macros/lib.rs | 39 ++++++++++++++++
rust/macros/module.rs | 4 +-
15 files changed, 237 insertions(+), 20 deletions(-)
---
base-commit: 89c07d98716a13454ec3fd9f97689e812cc71bd4
change-id: 20260828-cv-acaf3400d16c
Best regards,
--
Gary Guo <gary@garyguo.net>
On Thu, Sep 3, 2026 at 5:21 PM Gary Guo <gary@garyguo.net> wrote: > > Add a mechanism to perform const trait method calls, with the same type > inference capabilities. This would only work for concrete types (not inside > generic functions). > > As an example, convert `as_char_ptr_in_const_context`, so instead of > > kernel::str::as_char_ptr_in_const_context(c_str) > > one may write > > const_call!(c_str.as_char_ptr()) > > instead. > > Signed-off-by: Gary Guo <gary@garyguo.net> Thanks for resending this! So I definitely like having `#[const_eval_only]`, because we do want to limit those calls sometimes and it is nice to have C++'s equivalent to `consteval` (perhaps this could be called just `#[consteval]` to match?); i.e. what we talked about in the call. On `Const` and `const_call!`, i.e. the other two patches: I am not sure if we care that much to introduce a macro for it, but it doesn't look bad either. I guess perhaps it helps to have a consistent way to do it and to easily remove it later when the actual feature lands in Rust. I would like to hear some thoughts on it from others. Cheers, Miguel
On Thu Sep 3, 2026 at 4:34 PM BST, Miguel Ojeda wrote:
> On Thu, Sep 3, 2026 at 5:21 PM Gary Guo <gary@garyguo.net> wrote:
>>
>> Add a mechanism to perform const trait method calls, with the same type
>> inference capabilities. This would only work for concrete types (not inside
>> generic functions).
>>
>> As an example, convert `as_char_ptr_in_const_context`, so instead of
>>
>> kernel::str::as_char_ptr_in_const_context(c_str)
>>
>> one may write
>>
>> const_call!(c_str.as_char_ptr())
>>
>> instead.
>>
>> Signed-off-by: Gary Guo <gary@garyguo.net>
>
> Thanks for resending this!
>
> So I definitely like having `#[const_eval_only]`, because we do want
> to limit those calls sometimes and it is nice to have C++'s equivalent
> to `consteval` (perhaps this could be called just `#[consteval]` to
> match?); i.e. what we talked about in the call.
>
> On `Const` and `const_call!`, i.e. the other two patches: I am not
> sure if we care that much to introduce a macro for it, but it doesn't
> look bad either. I guess perhaps it helps to have a consistent way to
> do it and to easily remove it later when the actual feature lands in
> Rust. I would like to hear some thoughts on it from others.
I think it's good to have such macro compared to our current approach; it can be
made into a identity macro once we have const trait impl.
FWIW, I even considered an extra macro that allow you to write
#[const_trait_impl] // #[const_trait_impl(foreign)] for core types
impl MyTrait for MyType {}
and generate the `impl Const` part too (and also add a line to the documentation
that the method is const-callable with `const_call!()`).
Best,
Gary
On Fri Sep 4, 2026 at 12:46 AM JST, Gary Guo wrote:
> On Thu Sep 3, 2026 at 4:34 PM BST, Miguel Ojeda wrote:
>> On Thu, Sep 3, 2026 at 5:21 PM Gary Guo <gary@garyguo.net> wrote:
>>>
>>> Add a mechanism to perform const trait method calls, with the same type
>>> inference capabilities. This would only work for concrete types (not inside
>>> generic functions).
>>>
>>> As an example, convert `as_char_ptr_in_const_context`, so instead of
>>>
>>> kernel::str::as_char_ptr_in_const_context(c_str)
>>>
>>> one may write
>>>
>>> const_call!(c_str.as_char_ptr())
>>>
>>> instead.
>>>
>>> Signed-off-by: Gary Guo <gary@garyguo.net>
>>
>> Thanks for resending this!
>>
>> So I definitely like having `#[const_eval_only]`, because we do want
>> to limit those calls sometimes and it is nice to have C++'s equivalent
>> to `consteval` (perhaps this could be called just `#[consteval]` to
>> match?); i.e. what we talked about in the call.
>>
>> On `Const` and `const_call!`, i.e. the other two patches: I am not
>> sure if we care that much to introduce a macro for it, but it doesn't
>> look bad either. I guess perhaps it helps to have a consistent way to
>> do it and to easily remove it later when the actual feature lands in
>> Rust. I would like to hear some thoughts on it from others.
>
> I think it's good to have such macro compared to our current approach; it can be
> made into a identity macro once we have const trait impl.
>
> FWIW, I even considered an extra macro that allow you to write
>
> #[const_trait_impl] // #[const_trait_impl(foreign)] for core types
> impl MyTrait for MyType {}
>
> and generate the `impl Const` part too (and also add a line to the documentation
> that the method is const-callable with `const_call!()`).
>
> Best,
> Gary
I agree with the consteval macro, that sounds very nice.
On the const_call! I have similar thoughts to Miguel. I reckon if we
have a bunch of prospective users of this right now, and some thoughts
on what would use this in the future then it's good. If it's just one
use case right now, then not so sure.
On Mon Sep 7, 2026 at 10:21 AM BST, Eliot Courtney wrote:
> On Fri Sep 4, 2026 at 12:46 AM JST, Gary Guo wrote:
>> On Thu Sep 3, 2026 at 4:34 PM BST, Miguel Ojeda wrote:
>>> On Thu, Sep 3, 2026 at 5:21 PM Gary Guo <gary@garyguo.net> wrote:
>>>>
>>>> Add a mechanism to perform const trait method calls, with the same type
>>>> inference capabilities. This would only work for concrete types (not inside
>>>> generic functions).
>>>>
>>>> As an example, convert `as_char_ptr_in_const_context`, so instead of
>>>>
>>>> kernel::str::as_char_ptr_in_const_context(c_str)
>>>>
>>>> one may write
>>>>
>>>> const_call!(c_str.as_char_ptr())
>>>>
>>>> instead.
>>>>
>>>> Signed-off-by: Gary Guo <gary@garyguo.net>
>>>
>>> Thanks for resending this!
>>>
>>> So I definitely like having `#[const_eval_only]`, because we do want
>>> to limit those calls sometimes and it is nice to have C++'s equivalent
>>> to `consteval` (perhaps this could be called just `#[consteval]` to
>>> match?); i.e. what we talked about in the call.
I'm okay with either. I put in `_only` because I think it makes it very clear
that this is not just const-evalutable (that's provided by `const`) and is
*always* const evaled.
>>>
>>> On `Const` and `const_call!`, i.e. the other two patches: I am not
>>> sure if we care that much to introduce a macro for it, but it doesn't
>>> look bad either. I guess perhaps it helps to have a consistent way to
>>> do it and to easily remove it later when the actual feature lands in
>>> Rust. I would like to hear some thoughts on it from others.
>>
>> I think it's good to have such macro compared to our current approach; it can be
>> made into a identity macro once we have const trait impl.
>>
>> FWIW, I even considered an extra macro that allow you to write
>>
>> #[const_trait_impl] // #[const_trait_impl(foreign)] for core types
>> impl MyTrait for MyType {}
>>
>> and generate the `impl Const` part too (and also add a line to the documentation
>> that the method is const-callable with `const_call!()`).
>>
>> Best,
>> Gary
>
> I agree with the consteval macro, that sounds very nice.
>
> On the const_call! I have similar thoughts to Miguel. I reckon if we
> have a bunch of prospective users of this right now, and some thoughts
> on what would use this in the future then it's good. If it's just one
> use case right now, then not so sure.
Some other candidates include `Alignable` (we currently have a "const_align_up")
and maybe `IntoSafeCast`.
Best,
Gary
On Wed Sep 9, 2026 at 11:32 PM JST, Gary Guo wrote:
> On Mon Sep 7, 2026 at 10:21 AM BST, Eliot Courtney wrote:
>> On Fri Sep 4, 2026 at 12:46 AM JST, Gary Guo wrote:
>>> On Thu Sep 3, 2026 at 4:34 PM BST, Miguel Ojeda wrote:
>>>> On Thu, Sep 3, 2026 at 5:21 PM Gary Guo <gary@garyguo.net> wrote:
>>>>>
>>>>> Add a mechanism to perform const trait method calls, with the same type
>>>>> inference capabilities. This would only work for concrete types (not inside
>>>>> generic functions).
>>>>>
>>>>> As an example, convert `as_char_ptr_in_const_context`, so instead of
>>>>>
>>>>> kernel::str::as_char_ptr_in_const_context(c_str)
>>>>>
>>>>> one may write
>>>>>
>>>>> const_call!(c_str.as_char_ptr())
>>>>>
>>>>> instead.
>>>>>
>>>>> Signed-off-by: Gary Guo <gary@garyguo.net>
>>>>
>>>> Thanks for resending this!
>>>>
>>>> So I definitely like having `#[const_eval_only]`, because we do want
>>>> to limit those calls sometimes and it is nice to have C++'s equivalent
>>>> to `consteval` (perhaps this could be called just `#[consteval]` to
>>>> match?); i.e. what we talked about in the call.
>
> I'm okay with either. I put in `_only` because I think it makes it very clear
> that this is not just const-evalutable (that's provided by `const`) and is
> *always* const evaled.
>
>>>>
>>>> On `Const` and `const_call!`, i.e. the other two patches: I am not
>>>> sure if we care that much to introduce a macro for it, but it doesn't
>>>> look bad either. I guess perhaps it helps to have a consistent way to
>>>> do it and to easily remove it later when the actual feature lands in
>>>> Rust. I would like to hear some thoughts on it from others.
>
>>>
>>> I think it's good to have such macro compared to our current approach; it can be
>>> made into a identity macro once we have const trait impl.
>>>
>>> FWIW, I even considered an extra macro that allow you to write
>>>
>>> #[const_trait_impl] // #[const_trait_impl(foreign)] for core types
>>> impl MyTrait for MyType {}
>>>
>>> and generate the `impl Const` part too (and also add a line to the documentation
>>> that the method is const-callable with `const_call!()`).
>>>
>>> Best,
>>> Gary
>>
>> I agree with the consteval macro, that sounds very nice.
>>
>> On the const_call! I have similar thoughts to Miguel. I reckon if we
>> have a bunch of prospective users of this right now, and some thoughts
>> on what would use this in the future then it's good. If it's just one
>> use case right now, then not so sure.
>
> Some other candidates include `Alignable` (we currently have a "const_align_up")
> and maybe `IntoSafeCast`.
>
> Best,
> Gary
I'm definitely not opposed to a const trait polyfill, it's pretty cool
actually IMO. I also do think there is a bit of survivorship bias on the
argument (which I made) that there are not many users. And it can be
removed once const traits become available in MSRV. I am keen to hear
from others on this.
© 2016 - 2026 Red Hat, Inc.