Core Library0.xAPI reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice

method libtmux-rs

QueryIteratorExt.exactly_one

Return the only item, or an error for zero or multiple items.

Source and package
Qualified name
libtmux::query::QueryIteratorExt::exactly_one
Module
query
Declared in
QueryIteratorExt
Package
libtmux
Source
crates/libtmux/src/query.rs
#[allow(clippy::module_name_repetitions)] pub trait QueryIteratorExt: Iterator + Sized fn exactly_one(mut self) -> Result< Self::Item, ExactlyOneError >
Errors
Returns ExactlyOneError::NoItems for an empty iterator and ExactlyOneError::MultipleItems when a second item is present.doc comment · query.rs:564

Description

At most two items are pulled from the iterator.

Errors

Returns ExactlyOneError::NoItems for an empty iterator and ExactlyOneError::MultipleItems when a second item is present.

Examples

use libtmux::query::QueryIteratorExt;
let values = [7];
assert_eq!(values.iter().exactly_one(), Ok(&values[0]));

Handle missing and ambiguous matches. Compare exactly_one and one_or_none for no match, one match and multiple matches. Use Rust 1.85 or newer with Cargo, and tmux 3.3a or newer, on a Unix system. In an empty directory, create a project and add the published crates:

Terminal window
$ cargo init --name libtmux-api-example &&
cargo add libtmux@0.1.2 tempfile@3.20 &&
cargo add tokio@1.40 --features macros,rt,time

Replace src/main.rs with this complete program. It owns a private tmux socket, applies a ten-second task deadline, and reports both task and cleanup errors. Server::kill() stops its daemon; Server::shutdown() closes its client executor. Successful cleanup removes the temporary directory even when the task fails. View source

src/main.rsComplete program
//! Distinguish no match, one match and multiple matches.
use std::error::Error;
use std::time::Duration;
use libtmux::query::{ExactlyOneError, Filterable as _, MultipleItemsError, QueryIteratorExt as _};
use libtmux::{NewSessionOptions, Server, Session};
type ExampleError = Box<dyn Error>;
fn check(condition: bool, message: &str) -> Result<(), ExampleError> {
if condition {
Ok(())
} else {
Err(message.into())
}
}
async fn demonstrate(server: &Server) -> Result<(), ExampleError> {
server
.new_session(NewSessionOptions::new("work").command("cat"))
.await?;
server
.new_session(NewSessionOptions::new("review").command("cat"))
.await?;
let sessions = server.sessions().await?;
let fields = Session::filter_fields();
let work = fields.session_name.eq("work");
let missing = fields.session_name.eq("missing");
check(
sessions
.iter()
.matching(&work)
.exactly_one()?
.name()
.as_bytes()
== b"work",
"wrong single match",
)?;
check(
sessions.iter().matching(&missing).one_or_none()?.is_none(),
"missing match was present",
)?;
check(
matches!(
sessions.iter().matching(&missing).exactly_one(),
Err(ExactlyOneError::NoItems)
),
"missing match did not report NoItems",
)?;
check(
matches!(
sessions.iter().exactly_one(),
Err(ExactlyOneError::MultipleItems)
),
"ambiguous match was accepted",
)?;
check(
matches!(sessions.iter().one_or_none(), Err(MultipleItemsError)),
"optional lookup accepted multiple matches",
)?;
println!("cardinality: none, one, multiple");
Ok(())
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), ExampleError> {
let root = std::path::Path::new("/tmp/libtmux-rs-dev");
std::fs::create_dir_all(root)?;
let directory = tempfile::Builder::new()
.prefix("api-cardinality-")
.tempdir_in(root)?;
std::fs::write(
directory.path().join("owner"),
std::process::id().to_string(),
)?;
let server = Server::builder()
.socket_path(directory.path().join("tmux.sock"))
.config_file("/dev/null")
.default_timeout(Duration::from_secs(5))
.build()?;
let outcome = tokio::time::timeout(Duration::from_secs(10), demonstrate(&server)).await;
// Stop the owned daemon before closing the client executor.
let killed = server.kill().await;
let closed = server.shutdown().await;
let cleanup_failed = killed.is_err() || closed.is_err();
let mut failures = Vec::new();
match outcome {
Ok(Ok(())) => {}
Ok(Err(error)) => failures.push(format!("example failed: {error}")),
Err(error) => failures.push(format!("example deadline: {error}")),
}
if let Err(error) = killed {
failures.push(format!("daemon cleanup: {error}"));
}
if let Err(error) = closed {
failures.push(format!("executor cleanup: {error}"));
}
if cleanup_failed {
let retained = directory.keep();
failures.push(format!("inspect retained directory {}", retained.display()));
} else if let Err(error) = directory.close() {
failures.push(format!("directory cleanup: {error}"));
}
if failures.is_empty() {
Ok(())
} else {
Err(failures.join("; ").into())
}
}

Run the consumer. Success exits with status 0; setup, operation, deadline or cleanup failures exit unsuccessfully and print diagnostics to stderr:

Terminal window
$ cargo run --quiet

Expected output:

cardinality: none, one, multiple

Recorded calls

source calls
No call edges in the Rust model.

The Rust reference records declarations, not calls, so this page shows no callers or callees. That is a limit of the extracted data, not a statement that nothing calls query.QueryIteratorExt.exactly_one.

Guides

task material

Pages that discuss exactly_one.

Esc
Search in

Type to search.

Reference previewOpen reference