conduit_core/lib.rs
1#![forbid(unsafe_code)]
2#![deny(missing_docs)]
3//! # conduit-core
4//!
5//! Binary IPC core for Tauri v2. Provides a binary codec, synchronous dispatch
6//! table, and in-process ring buffer for the `conduit://` custom protocol.
7//!
8//! ## Dependency note
9//!
10//! This crate depends on `serde` and `sonic-rs` unconditionally. These are
11//! required by the [`Router`] JSON handler methods, the [`ConduitHandler`]
12//! trait (which powers `#[tauri_conduit::command]`), and the [`Error::Serialize`]
13//! variant. The pure binary codec ([`Encode`]/[`Decode`], [`RingBuffer`],
14//! [`Queue`]) does not use JSON at runtime, but the types are not
15//! feature-gated because the handler system is considered a core part of
16//! conduit's purpose.
17
18pub mod channel;
19pub mod codec;
20pub mod error;
21pub mod handler;
22pub mod queue;
23pub mod ringbuf;
24pub mod router;
25
26pub use channel::ChannelBuffer;
27pub use codec::{
28 Bytes, DRAIN_FRAME_OVERHEAD, Decode, Encode, FRAME_HEADER_SIZE, FrameHeader, MsgType,
29 PROTOCOL_VERSION, frame_pack, frame_unpack,
30};
31pub use error::Error;
32pub use handler::{ConduitHandler, HandlerContext, HandlerResponse};
33pub use queue::Queue;
34pub use ringbuf::{PushOutcome, RingBuffer};
35pub use router::Router;
36
37/// Acquire a [`Mutex`](std::sync::Mutex) lock, recovering from poison if
38/// another thread panicked while holding it.
39///
40/// Conduit's buffers are designed to remain usable after a panic — the data
41/// in a poisoned mutex is still valid. This helper avoids copy-pasting the
42/// `.unwrap_or_else(|e| e.into_inner())` pattern.
43#[inline]
44pub fn lock_or_recover<T>(mutex: &std::sync::Mutex<T>) -> std::sync::MutexGuard<'_, T> {
45 mutex.lock().unwrap_or_else(|e| e.into_inner())
46}
47
48/// Acquire a [`RwLock`](std::sync::RwLock) write guard, recovering from
49/// poison.
50#[inline]
51pub fn write_or_recover<T>(lock: &std::sync::RwLock<T>) -> std::sync::RwLockWriteGuard<'_, T> {
52 lock.write().unwrap_or_else(|e| e.into_inner())
53}
54
55/// Acquire a [`RwLock`](std::sync::RwLock) read guard, recovering from
56/// poison.
57#[inline]
58pub fn read_or_recover<T>(lock: &std::sync::RwLock<T>) -> std::sync::RwLockReadGuard<'_, T> {
59 lock.read().unwrap_or_else(|e| e.into_inner())
60}
61
62#[doc(hidden)]
63pub use serde;
64
65#[doc(hidden)]
66pub use sonic_rs;