Dear people,
I try to figure out what I use when I use the statement:
use stm32f3_discovery::stm32f3xx_hal::interrupt;
Because in the crate's documentation are an enum and a macro defined with the same name and the same case, so all in lower case, interrupt.
How do I figure out what I import?
Kind regards, Stephen.
What are you trying to do? If what you need is to define an ISR, have a look at this example:
I am trying to figure out how this works, so if there are two identical names in the crate documentation, but for different type items, then what do I import, the macro or the enum?
if you are asking about the language rules, the answer is both: use create a local shortcut of a path, not item or type, which applies to all namespaces, and macros and types are from different namespaces.
see:
In the language rules I can only use one name for a thing, not the same name for two different things.
you can hav
you can have different "things" with same name, as long they reside in different namespaces.
there's no ambiguity, because the namespace is context dependent. for example, if you call a function, the function name is looked up in the value namespace, it will NOT conflict with a struct with the same name, because structs are in the type namespace. macros are yet another separate namespace. read the linked reference page for details.
struct foo {}
fn foo() {}
fn main() {
// in `type` context, `foo` refers to the struct name:
let _: foo = todo!();
// in function call, `foo` refers to the function name:
foo();
}
note, a tuple struct defines both a type and its constructor function with the same name. this is not a violation to the rule, just a special case.
I am wondering about why you can have both macro and enum with the same name in one crate.
Not about what you are decribing. Off topic.
the author can choose whatever valid names they like. another similar example is the standard library, where std::env is both a macro and a module at the same time.
as for the interrupt case, it's actually a well-established convention seen in many bare metal targets, particularly, those generated using svd2rust.
I don't know for sure, but I think it's probably for compatibility reason, since it's always an alias of enum Interrupt in all the crates that I have used (rp2024, nrf52840, stm32f4xx). using lower case name (interrupt) for an enum is not idiomatic in rust, especially in public API, and user code usually want to use the camel case name (Interrupt).
I have figured it out, and I wonder why I didn't thought of that before.
I just mouse hovered over the use statement's last word, interrupt, in VCS, and rust-analyzer figured it out.
It is the enum. Which makes sense, because it has to know the interrupt in this code, since it switches a led on or off, via the on PCB user button.
Now I want to know how I use the macro interrupt. But I call it a day.
The macro is used explicitly,
#[interrupt]
fn button_interrupt() { ... do stuff... }
I think they just come together, the macro has to know what kind of MCU periherals there are, and the signals they might give, while peripherals vary per MCU, so I might have found a general way of putting together these crates, since all MCU's work generically the same, peripherals generate signals. And that's why they both have the same name, you always have to indicated that your function handles an event via the interrupt macro. Not sure ... I will find out. I will discover later what kind of implicit Rust Embedded way is behind this treat.
But, I again, it was enough for today.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.