Register

Trait Register 

Source
pub trait Register:
    HasCallbacks
    + Send
    + Sync
    + 'static {
    // Provided methods
    fn register_default(
        &mut self,
        id: PluginId,
        args: &Args,
        info: &Info,
    ) -> Result<()> { ... }
    fn register(&mut self, id: PluginId, args: &Args, info: &Info) -> Result<()> { ... }
}
Expand description

Trait which implemenents registering the callbacks implemented on a struct which HasCallbacks with QEMU

§Example

Using default registration, you can simply declare an empty impl block for Register on your type. Then, on plugin load, any callbacks you implement in HasCallbacks will be automatically registered with QEMU. Callback events you don’t implement will default to no-ops. The only drawback of this approach is a small performance penalty for events even when there is no callback registered for them.

struct MyPlugin;

impl qemu_plugin::plugin::HasCallbacks for MyPlugin {
    fn on_translation_block_translate(&mut self, _: qemu_plugin::PluginId, tb: qemu_plugin::TranslationBlock) -> qemu_plugin::Result<()> {
        println!("Translation block translated");
        Ok(())
    }
}

impl qemu_plugin::plugin::Register for MyPlugin {}

For more granular control or to register your own callback handlers, you can implement the register method yourself.

struct MyPlugin;

impl qemu_plugin::plugin::HasCallbacks for MyPlugin {}
impl qemu_plugin::plugin::Register for MyPlugin {
   fn register(&mut self, id: qemu_plugin::PluginId, args: &qemu_plugin::install::Args, info: &qemu_plugin::install::Info) -> Result<(), qemu_plugin::Error> {
      // Custom registration logic here
      Ok(())
   }
}

Finally, if you want to override the default registration behavior, you can implement register_default yourself. This allows you to circumvent any minor performance penalties.

struct MyPlugin;

impl qemu_plugin::plugin::HasCallbacks for MyPlugin {}
impl qemu_plugin::plugin::Register for MyPlugin {
    fn register_default(
       &mut self,   
       id: qemu_plugin::PluginId,
       args: &qemu_plugin::install::Args,
       info: &qemu_plugin::install::Info,
    ) -> Result<()> {
        // Custom registration logic here, maybe registering a different
        // function as a callback rather than using `HasCallbacks`
        Ok(())
   }
}

Provided Methods§

Source

fn register_default( &mut self, id: PluginId, args: &Args, info: &Info, ) -> Result<()>

Called by QEMU when registering the plugin. This method should only be overridden if no default callbacks are desired, and will require re-implementing handlers which is not recommended.

Source

fn register(&mut self, id: PluginId, args: &Args, info: &Info) -> Result<()>

Called when registering the plugin. User definition of on-registration behavior should be implemented here.

Implementors§