Skip to content

Add Typed Version of add_route. #242

@gatoWololo

Description

@gatoWololo

Currently add_route is defined as:

pub fn add_route(&self, receiver: OpaqueIpcReceiver, callback: RouterHandler);
pub type RouterHandler = Box<FnMut(OpaqueIpcMessage) + Send>;

It is up to the user to remember and properly turn the OpaqueIpcMessage into the correct type. This leads to wrong types being used as in servo/servo#23818

Instead I recommend adding a typed version as:

pub fn add_route<T>(&self, receiver: IpcReceiver<T>, mut callback: RouterHandler<T>)

The user no longer has to learn about OpaqueIpcReceiver and it is compile-time checked, so the type can never be used wrong! Internally, the implementation converts the OpaqueIpcMessage into T so it can never be misused:

|msg: OpaqueIpcMessage| callback(msg.to::<T>().unwrap());

Eventually the existing add_route could be deprecated in favor of the typed one.

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