Skip to main content

Module single_instance

Module single_instance 

Source
Expand description

One running instance per user: a second launch forwards its arguments (the files it was asked to open) to the first over a local socket, then exits.

let args: Vec<String> = std::env::args().skip(1).collect();
match SingleInstance::new("com.example.editor").claim(&args, |msg| {
    // On a listener thread: queue `msg.args` (resolved against `msg.cwd`) and wake the UI.
}) {
    Ok(Launch::Forwarded) => std::process::exit(0),
    Ok(Launch::Primary(guard)) => { /* keep `guard` alive while the app runs */ }
    Err(e) => { /* no single-instance support here: just run */ }
}

Transport: a Unix domain socket (macOS, Linux) in a per-user directory, readable only by the user; on Windows a loopback TCP port written to a per-user file with a random token the second launch must present. Messages are one JSON line, at most MAX_MESSAGE bytes. The handshake has timeouts, so a hung first instance never hangs the second.

Structs§

Forwarded
What a second launch sent.
PrimaryGuard
Stops listening (and removes the socket) when dropped.
SingleInstance
Which app the instance belongs to, and where its endpoint lives.

Enums§

Launch
The outcome of SingleInstance::claim.

Constants§

MAX_MESSAGE
Largest forwarded message.

Functions§

valid_app_id
An app id is a reverse-DNS-like name: ASCII letters, digits, ., -, _; at most 64.