ltk

Function try_run

Source
pub fn try_run<A: App>(app: A) -> Result<(), RunError>
Expand description

Run the application, returning a typed error on init failure.

Same as run but recoverable: every fatal init step (Wayland connection, registry, calloop event loop, wl_compositor / wl_shm / xdg_wm_base bindings) is converted into a RunError variant the caller can match on. Once init succeeds the function blocks until the surface is closed and returns Ok(()). Errors from the dispatch loop itself (already on screen) still panic — they are non-recoverable since the surface state machine cannot be unwound cleanly from this entry point.

Use this when:

  • the application needs a graceful fallback path on systems without a Wayland session (CI runners, minimal containers, X11-only environments where ltk has no backend),
  • an embedder wants to log the specific protocol that’s missing instead of panicking with a generic stack trace,
  • the caller wants to retry / wait for a compositor to come up instead of aborting.

The standard run( app ) remains the simpler entry point for applications that always run on a known-good Wayland session.

match ltk::try_run( MyApp )
{
    Ok( () ) => {}
    Err( RunError::NoWaylandConnection( _ ) ) =>
    {
        eprintln!( "no compositor — falling back to stdio" );
        // run a CLI fallback…
    }
    Err( RunError::MissingProtocol { name, .. } ) =>
    {
        eprintln!( "compositor lacks `{name}` — refusing to start" );
        std::process::exit( 1 );
    }
    Err( e ) =>
    {
        eprintln!( "ltk init failed: {e}" );
        std::process::exit( 1 );
    }
}