-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HandlerFuncType trait #439
Add HandlerFuncType trait #439
Conversation
src/structures/idt.rs
Outdated
/// This method is only usable with the `abi_x86_interrupt` feature enabled. Without it, the | ||
/// unsafe [`Entry::set_handler_addr`] method has to be used instead. | ||
#[inline] | ||
pub fn set_handler_fn(&mut self, handler: &F) -> &mut EntryOptions { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For backwards compatibility, set_handler_fn
needs to take the handler by value, not by reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. I didn't realize that function pointers are Copy
, so it doesn't matter if it's owned or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own understanding (I'm fairly new to Rust), why does set_handler_fn
take the handler function by value? If there's nothing in set_handler_fn
that requires the function to be moved, isn't it better to take a reference so that the caller can keep using the argument? Or is it specifically because it's a function, there's never a reason to pass a function by reference, and you should always Copy as needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya this can be a confusing thing initially, especially because the function Traits (Fn
, FnMut
, FnOnce
) are often handled differently.
A type like fn(u8) -> u8
or extern "x86-interrupt" fn(InterruptStackFrame) -> !
is a function pointer, so it references a specific function. It this way, it is similar to &T
or *const T
in that it is:
- pointer sized
- a shared reference to something immutable.
So for the same reason we usually don't use &&T
or &*const T
, using &fn(u8) -> u8
is uncommon.
@josephlr how's that? |
Signed-off-by: Joe Richey <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!
Fixes #438