Skip to content

Trait for Entry handler function types? #438

@brandonchinn178

Description

@brandonchinn178

I wanted to write a helper that generalized the function to set for an entry, like:

fn init_entry<F>(entry: Entry<F>, handle_fn: F) {
    ...
}

but Entry.set_handler_fn is explicitly implemented for each HandlerFunc type separately, so I don't think I can provide an adequate type bound on F.

Instead of implementing Entry.set_handler_fn explicitly for each HandlerFunc type, why not provide a trait for all HandlerFunc types? e.g.

trait HandlerFuncType {
    fn to_handler_fn_addr(&self) -> VirtAddr;
}

macro_rules! impl_handler_func_type {
    ($f:ty) => {
        impl HandlerFuncType for $f {
            #[inline]
            fn to_handler_fn_addr(&self) -> x86_64::VirtAddr {
                VirtAddr::new(*self as u64)
            }
        }
    };
}

impl_handler_func_type!(HandlerFunc);
// all other HandlerFuncs

And then do

impl Entry {
    #[inline]
    pub fn set_handler_fn<F: HandlerFuncType>(&mut self, handler: &F) -> &mut EntryOptions {
        unsafe { self.set_handler_addr(handler.to_handler_fn_addr()) }
    }
}

Would this work? I think this is backwards-compatible, so I don't see any downsides to making this change. I'm fairly new to Rust, so let me know if I'm missing anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions