rust-osdev / x86_64

Library to program x86_64 hardware.
https://docs.rs/x86_64
Apache License 2.0
797 stars 132 forks source link

Allow passing index as :ident in set_general_handler macro #384

Open asoderman opened 2 years ago

asoderman commented 2 years ago

Hello, please consider this small QOL change to the set_general_handler macro.

This would allow the index to be specified by a const or variable e.g.

const PAGEFAULT: u8 = 0xe;

set_general_handler(idt, page_fault, PAGEFAULT);

instead of being forced to convert it to a range

set_general_handler(idt, page_fault, PAGEFAULT..PAGEFAULT)

I'm not sure if there's any way to make this change less repetitive by having $idx be either literal or ident. It might also be worth considering refactoring out the setting individual handler functionality (the $idx variants) into its own macro and allow users to pass a $range:ident to the current macro to avoid ambiguity.

Freax13 commented 2 years ago

Perhaps we could approach this from another angle: The SliceIndex trait is used in the standard library to allow indexing with multiple different types. This was already considered in https://github.com/rust-osdev/x86_64/pull/95#issuecomment-557496537 and later ruled out in https://github.com/rust-osdev/x86_64/pull/319 because SliceIndex is based on usize and not u8. Instead, perhaps we could implement our own trait InterruptDescriptorTableIndex that would be implemented on u8 as well as all the range types and use that type for the bounds checks in the macro. The same trait could probably be used for InterruptDescriptorTable::slice as well as InterruptDescriptorTable::index.