Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

[RFC] [breaking-change] Turn entry!, exception! into attributes #82

@japaric

Description

@japaric

What the title says. I believe attributes would be more ergonomic and less redundant. Compare this:

entry!(main);

fn main() -> ! {
    // .. user code ..
}

exception!(HardFault, handler);

fn handler() -> ! {
    // .. user code ..
}

to this:

#[entry]
fn main() -> ! {
    // .. user code ..
}

#[exception(HardFault)]
fn handler() -> ! {
    // .. user code ..
}

As a bonus point we get rid of the indirection that the entry! macro requires.

entry! expands into this for type safety:

#[export_name = "main"]
fn __impl_main() -> ! {
    // check the signature of `main`
    let f: fn() -> ! = main;
    f()
}

The function call doesn't always get inlined (unless you marked main as #[inline(always)], which
looks a bit odd).

On the other hand, #[entry] expands into this:

#[export_name = "main"]
pub fn main() -> ! {
    // .. user code ..
}

so there's no function call even when compiling without optimizations. (The type check is done
syntactically during the macro expansion)

Unresolved questions

  • Choose a syntax for adding state to #[exception]s.

#[proc_macro_attribute], which is required to write these macros, has already been stabilized in
nightly. #![feature(use_extern_macros)], which is required to use these macros, has not been
stabilized yet, but it's planned for the 2018 edition (1.31).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions