Skip to main content

Crate single_instance

Crate single_instance 

Source
Expand description

A rust library for single instance application.

single-instance provides a single API to check if there are any other running instance.

§Detail

On Linux and Android, SingleInstance::new binds an abstract unix domain socket with the given name. On Apple platforms (macOS, iOS, tvOS, watchOS), it creates or opens the file at the given path and takes an exclusive advisory lock on it.

§Examples

extern crate single_instance;

use std::thread;
use single_instance::SingleInstance;

fn main() {
    let instance = SingleInstance::new("whatever").unwrap();
    assert!(instance.is_single());
}
§Keeping the instance alive for the process lifetime

The SingleInstance must stay alive for as long as the exclusivity guarantee is needed, because dropping it releases the lock. Store it for the lifetime of the process, for example by leaking it:

use single_instance::SingleInstance;

fn ensure_single_instance(unique_id: &str) -> bool {
    let instance = Box::new(SingleInstance::new(unique_id).unwrap());
    if instance.is_single() {
        Box::leak(instance);
        true
    } else {
        false
    }
}

Structs§

SingleInstance
A struct representing one running instance.

Enums§

Error
The error returned when a SingleInstance cannot be created.