noib3 / nvim-oxi

:link: Rust bindings to all things Neovim
https://crates.io/crates/nvim-oxi
MIT License
904 stars 45 forks source link

Expose `Buffer` underlying raw id #173

Closed myypo closed 4 months ago

myypo commented 5 months ago

Hi. I have encountered a need to use the Buffer struct's underlying BufHandle as a raw i32.

Just to avoid the XY problem: I need to be able to format it as an i32 to pass it in a buffer-local set_keymap as right side of the keymap that invokes another plugin function. I can't use a callback instead of rhs for this because my plugin's functions rely on state from a separate non-nvim thread that the plugin spawns on startup.

Since it is impossible to implement a non-conflicting From/Into because of this implementation:

impl<H: Into<BufHandle>> From<H> for Buffer {
    #[inline(always)]
    fn from(handle: H) -> Self {
        Self(handle.into())
    }
}

What I came up with so far is such method:

pub fn as_i32(&self) -> i32 {
    self.0
}
noib3 commented 4 months ago

Yes, we could add a fn handle(&self) -> i32 method on Buffer, Window and TabPage. I'd merge a PR if you want to work on adding those.

P.S. all the handles are i32 because Neovim defines handle_T as an int, but are negative handles ever used?

myypo commented 4 months ago

Yes, we could add a fn handle(&self) -> i32 method on Buffer, Window and TabPage. I'd merge a PR if you want to work on adding those.

Ok, I will work on it then.

P.S. all the handles are i32 because Neovim defines handle_T as an int, but are negative handles ever used?

I haven't ever encountered negative handles but I think playing safe and just using i32 won't hurt.

noib3 commented 4 months ago

Closed by #176.