Sometimes when dealing with re-entrant code your only option is to use Cell<Option>, and unfortunately those are a pain to work with.
Unfortunately Cell.set is already taken, so it'd have to be something like Cell.put for the Option variant. (At least Cell.take already uses Default and works with Option out of the box, so on that end it's a non-issue.)
Can we have Cell<Option<T>>.put?
Motivation:
#[macro_use]
extern crate hexchat_plugin;
use hexchat_plugin::Plugin as HexchatPlugin;
use hexchat_plugin::PluginHandle as Ph;
use hexchat_plugin::CommandHookHandle;
#[derive(Default)]
struct Cw2Plugin {
cmd_cw: Cell<Option<CommandHookHandle>>,
}
impl HexchatPlugin for Cw2Plugin {
fn init(&self, ph: &mut Ph, _arg: Option<&str>) -> bool {
ph.register("CW2", "1.0.0", "Adds support for rot13-based content warnings.");
self.cmd_cw.set(Some(ph.hook_command("cw", |ph, arg, arg_eol| {
todo!()
}, 0, Some("Sends a content warning. Usage: "))));
true
}
}
hexchat_plugin!(Cw2Plugin);
It's two right parens. There are situations where there are far more than that and nobody blinks. I agree with @CAD97 — create a newtype if it's that important to you.
Keep in mind that this would only solve one situations; what would happen if someone then wanted Cell<Result<T, E>>? Add a new method for that? What about Either<T>? The requests would never end.