bon/collections.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/// Same as [`std::vec!`] but converts each element with [`Into`].
///
/// **WARNING:** it's not recommended to import this macro into scope. Reference it
/// using the full path (`bon::vec![]`) to avoid confusion with the [`std::vec!`] macro.
///
/// A good example of the use case for this macro is when you want to create a
/// [`Vec<String>`] where part of the items are hard-coded string literals of type
/// `&str` and the other part is made of dynamic [`String`] values.
///
/// ```
/// fn convert_media(input_extension: &str, output_extension: &str) -> std::io::Result<()> {
/// let ffmpeg_args: Vec<String> = bon::vec![
/// "-i",
/// format!("input.{input_extension}"),
/// "-y",
/// format!("output.{output_extension}"),
/// ];
///
/// std::process::Command::new("ffmpeg").args(ffmpeg_args).output()?;
///
/// Ok(())
/// }
/// ```
///
/// This macro doesn't support `vec![expr; N]` syntax, since it's simpler to
/// just write `vec![expr.into(); N]` using [`std::vec!`] instead.
#[macro_export]
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
#[allow(edition_2024_expr_fragment_specifier)]
macro_rules! vec {
() => ($crate::__::alloc::vec::Vec::new());
($($item:expr),+ $(,)?) => ($crate::__::alloc::vec![$(::core::convert::Into::into($item)),+ ]);
}
/// Creates a fixed-size array literal with each element converted with [`Into`].
///
/// You'll probably need a hint for the target type of items in the array if the
/// compiler can't infer it from its usage.
///
/// This is similar in spirit to the [`bon::vec!`] macro, but it's for arrays.
/// See [`bon::vec!`] docs for details.
///
/// Same example as in [`bon::vec!`], but using this macro. It works with array
/// as well because [`Command::args`] accepts any value that implements [`IntoIterator`]:
///
/// ```
/// fn convert_media(input_extension: &str, output_extension: &str) -> std::io::Result<()> {
/// let ffmpeg_args: [String; 4] = bon::arr![
/// "-i",
/// format!("input.{input_extension}"),
/// "-y",
/// format!("output.{output_extension}"),
/// ];
///
/// std::process::Command::new("ffmpeg").args(ffmpeg_args).output()?;
///
/// Ok(())
/// }
/// ```
///
/// This macro doesn't support `[expr; N]` syntax, since it's simpler to
/// just write `[expr.into(); N]` instead.
///
/// [`Command::args`]: std::process::Command::args
/// [`bon::vec!`]: crate::vec
#[macro_export]
#[allow(edition_2024_expr_fragment_specifier)]
macro_rules! arr {
() => ([]);
($($item:expr),+ $(,)?) => ([$(::core::convert::Into::into($item)),+]);
}
#[cfg(test)]
mod tests {
#[cfg(feature = "alloc")]
use crate::__::alloc::{string::String, vec::Vec};
use core::num::NonZeroU8;
#[cfg(feature = "alloc")]
#[test]
fn arr_of_strings() {
let actual: [String; 3] = crate::arr!["foo", "bar", "baz"];
assert_eq!(actual, ["foo", "bar", "baz"]);
let actual: [String; 0] = crate::arr![];
assert!(actual.is_empty());
}
#[test]
fn arr_of_numbers() {
let actual: [u8; 2] = crate::arr![NonZeroU8::new(1).unwrap(), NonZeroU8::new(2).unwrap()];
assert_eq!(actual, [1, 2]);
let actual: [u8; 0] = crate::arr![];
assert!(actual.is_empty());
}
#[cfg(feature = "alloc")]
#[test]
fn vec_smoke() {
let actual: Vec<String> = crate::vec!["foo", "bar", "baz"];
assert_eq!(actual, ["foo", "bar", "baz"]);
let actual: Vec<String> = crate::vec![];
assert!(actual.is_empty());
}
#[cfg(feature = "std")]
#[test]
fn map_smoke() {
use std::collections::{BTreeMap, HashMap};
let hash_strings: HashMap<String, String> = crate::map! {
"Hello": "World",
"Goodbye": "Mars",
};
assert_eq!(hash_strings["Hello"], "World");
assert_eq!(hash_strings["Goodbye"], "Mars");
let tree_strings: BTreeMap<String, String> = crate::map! {
"Hello": "World",
"Goodbye": "Mars",
};
assert_eq!(tree_strings["Hello"], "World");
assert_eq!(tree_strings["Goodbye"], "Mars");
}
#[cfg(feature = "std")]
#[test]
fn set_smoke() {
use std::collections::BTreeSet;
use std::collections::HashSet;
let hash_strings: HashSet<String> = crate::set!["Hello", "World", "Goodbye", "Mars"];
assert!(hash_strings.contains("Hello"));
assert!(hash_strings.contains("World"));
assert!(hash_strings.contains("Goodbye"));
assert!(hash_strings.contains("Mars"));
let tree_strings: BTreeSet<String> = crate::set!["Hello", "World", "Goodbye", "Mars"];
assert!(tree_strings.contains("Hello"));
assert!(tree_strings.contains("World"));
assert!(tree_strings.contains("Goodbye"));
assert!(tree_strings.contains("Mars"));
}
}