From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- third_party/rust/smart-default/src/util.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 third_party/rust/smart-default/src/util.rs (limited to 'third_party/rust/smart-default/src/util.rs') diff --git a/third_party/rust/smart-default/src/util.rs b/third_party/rust/smart-default/src/util.rs new file mode 100755 index 0000000000..452ac91f95 --- /dev/null +++ b/third_party/rust/smart-default/src/util.rs @@ -0,0 +1,29 @@ +use syn::spanned::Spanned; +use syn::parse::Error; + +/// Return the value that fulfills the predicate if there is one in the slice. Panic if there is +/// more than one. +pub fn find_only(iter: impl Iterator, pred: F) -> Result, Error> +where T: Spanned, + F: Fn(&T) -> Result, +{ + let mut result = None; + for item in iter { + if pred(&item)? { + if result.is_some() { + return Err(Error::new(item.span(), "Multiple defaults")); + } + result = Some(item); + } + } + Ok(result) +} + +pub fn single_value(mut it: impl Iterator) -> Option { + if let Some(result) = it.next() { + if it.next().is_none() { + return Some(result) + } + } + None +} -- cgit v1.2.3