cratelyn opened issue #3047:
Feature
Hi! :wave: I'd like to propose extending
cranelift_entity::entity_impl
to accept an expression that ought to be used to construct the given entity.Benefit
In certain corner cases, particularly when using libraries that generate code from
.witx
defintions, we want to useentity_impl!
with handle types that are not defined ourselves. Consequentially, we cannot always construct the handle directly via a statement like$entity(index as u32)
.I put together a Rust Playground example demonstrating the problem here, you could also refer to this gist.
:question: Implementation
This part I have some questions, and welcome advice. At a high level, I hoped for this to look something like this...
macro_rules! entity_impl { ($entity:ident) => { ... }; ($entity:ident, $display_prefix:expr) => { ... }; + ($entity:ident, $display_prefix:expr, $construct:expr) => { ... }; }
...which would allow an invocation shaped like:
entity_impl(ExampleHandle, "example-prefix-", (index as u32).into())
However, when experimenting with this, I ended up running into errors shaped like
cannot find value 'index' in this scope
. I tried alternative designators likestmt
, but didn't have much luck.I'm happy to carry out the fix for this, but might need a bit of help deciding how to present this change to callers.
bjorn3 commented on issue #3047:
You would need something like
|$index:ident| $construct:expr
and then define the$index
variableble instead ofindex
. In rust macros are hygienic, preventing names from flowing in or out of a macro body.
cfallin commented on issue #3047:
@cratelyn thanks for filing this issue!
I played with the Rust Playground for a bit and (as @bjorn3 notes above) got something to work by passing in an identifier to work with the hygiene. Something like:
the key bit there is the macro form that takes
($entity:ident, $display_prefix:expr, $arg:ident, $to_expr:expr, $from_expr:expr)
, with which we can then do something like:entity_impl!(ExternalHandle, "external-", i, ExternalHandle::from(i), u32::from(i));
Does that seem reasonable? Happy to review a PR if so!
cratelyn commented on issue #3047:
That makes sense to me @cfallin, thank you for taking a look! I'll put a patch together for you soon! :adhesive_bandage:
cfallin assigned issue #3047:
Feature
Hi! :wave: I'd like to propose extending
cranelift_entity::entity_impl
to accept an expression that ought to be used to construct the given entity.Benefit
In certain corner cases, particularly when using libraries that generate code from
.witx
defintions, we want to useentity_impl!
with handle types that are not defined ourselves. Consequentially, we cannot always construct the handle directly via a statement like$entity(index as u32)
.I put together a Rust Playground example demonstrating the problem here, you could also refer to this gist.
:question: Implementation
This part I have some questions, and welcome advice. At a high level, I hoped for this to look something like this...
macro_rules! entity_impl { ($entity:ident) => { ... }; ($entity:ident, $display_prefix:expr) => { ... }; + ($entity:ident, $display_prefix:expr, $construct:expr) => { ... }; }
...which would allow an invocation shaped like:
entity_impl(ExampleHandle, "example-prefix-", (index as u32).into())
However, when experimenting with this, I ended up running into errors shaped like
cannot find value 'index' in this scope
. I tried alternative designators likestmt
, but didn't have much luck.I'm happy to carry out the fix for this, but might need a bit of help deciding how to present this change to callers.
cfallin closed issue #3047 (assigned to cratelyn):
Feature
Hi! :wave: I'd like to propose extending
cranelift_entity::entity_impl
to accept an expression that ought to be used to construct the given entity.Benefit
In certain corner cases, particularly when using libraries that generate code from
.witx
defintions, we want to useentity_impl!
with handle types that are not defined ourselves. Consequentially, we cannot always construct the handle directly via a statement like$entity(index as u32)
.I put together a Rust Playground example demonstrating the problem here, you could also refer to this gist.
:question: Implementation
This part I have some questions, and welcome advice. At a high level, I hoped for this to look something like this...
macro_rules! entity_impl { ($entity:ident) => { ... }; ($entity:ident, $display_prefix:expr) => { ... }; + ($entity:ident, $display_prefix:expr, $construct:expr) => { ... }; }
...which would allow an invocation shaped like:
entity_impl(ExampleHandle, "example-prefix-", (index as u32).into())
However, when experimenting with this, I ended up running into errors shaped like
cannot find value 'index' in this scope
. I tried alternative designators likestmt
, but didn't have much luck.I'm happy to carry out the fix for this, but might need a bit of help deciding how to present this change to callers.
Last updated: Nov 22 2024 at 16:03 UTC