nvim_rs/examples/quitting.rs
1//! # Quitting
2//!
3//! A very small example showing how to handle quitting an application.
4//!
5//! * Running as an rpc plugin, just have neovim close the channel corresponding
6//! to our plugin
7//! * When embedding neovim, do the same via a command, as shown in this
8//! example. Note that this final request _will_ receive an error, since it will
9//! not get an answer from neovim.
10//!
11//! Also note that all other pending requests will receive an EOF error as well.
12//!
13//! ## Usage
14//!
15//! First, build the neovim included as a submodule:
16//!
17//! ```sh
18//! cd neovim
19//! make
20//! ```
21//!
22//! See <https://github.com/neovim/neovim/wiki/Building-Neovim> for build options.
23//! Nothing is really needed to run the example.
24//!
25//! After that, run the example via
26//!
27//! ```sh
28//! cargo run --example quitting
29//! ```
30//!
31//! ## Description
32//!
33//! Some overview over the code:
34//!
35//! * Since we're not interested in handling anything, our `NeovimHandler` is a
36//! [`Dummy`](crate::rpc::handler::Dummy) that does nothing on requests and
37//! notifications. We need to pass something that implements
38//! [`Spawn`](futures::task::Spawn), which is represented by the `Spawner`. It
39//! doesn't carry any data here. We implement `Spawn` in the most trivial way
40//! possible, by calling [`tokio::spawn`](tokio::spawn) that in turn calls out
41//! to the surrounding runtime.
42//!
43//! * Any shutdown logic should be handled after the channel was closed. We
44//! don't actually need to inspect the error, since the application will shut
45//! down no matter what. If we need access to our handler for that, we should
46//! implement [`Drop`](std::ops::Drop) for it, see
47//! [`handler_drop`](crate::examples::handler_drop).
48//!
49//! * The last command (the one that instructs neovim to close the channel) will
50//! not receive an answer anymore, but an error. We just show the error and its
51//! source for demonstation purposes. We use the
52//! [`is_channel_closed`](crate::error::CallError::is_channel_closed) method
53//! to verify that the error originates from this.