Skip to main content

collections/
macros.rs

1// SPDX-License-Identifier: Apache-2.0
2//
3// Written in 2022-2026 by Dr. Maxim Orlovsky <orlovsky@ubideco.org>
4//
5// Copyright 2022-2026 Laboratories for Ubiquitous and Deterministic Computing,
6// Institute for Distributed and Cognitive Computing (InDCS), Switzerland.
7// All rights reserved.
8//
9// Copyright (C) 2022-2026 Dr Maxim Orlovsky.
10// All rights reserved.
11//
12// Licensed under the Apache License, Version 2.0 (the "License"); you may not
13// use this file except in compliance with the License. You may obtain a copy of
14// the License at <http://www.apache.org/licenses/LICENSE-2.0>
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19// License for the specific language governing permissions and limitations under
20// the License.
21
22/// Converts a string literal into an allocated `String`.
23///
24/// ```
25/// use collections::s;
26///
27/// let text = s!("hello");
28/// assert_eq!(text, "hello");
29/// ```
30#[macro_export]
31macro_rules! s {
32    ($str:literal) => {
33        $crate::alloc::string::String::from($str)
34    };
35}
36
37/// Creates an allocated `BTreeMap` from key-value pairs.
38///
39/// The `owned:` form calls `to_owned()` on each key and value.
40///
41/// ```
42/// use collections::bmap;
43///
44/// let map = bmap! { 1 => "one", 2 => "two" };
45/// assert_eq!(map.len(), 2);
46///
47/// let owned = bmap! { owned: "key" => "value" };
48/// assert_eq!(owned["key"], "value");
49/// ```
50#[macro_export]
51macro_rules! bmap {
52    {} => {
53        $crate::alloc::collections::BTreeMap::new()
54    };
55
56    { owned: $($key:expr_2021 => $value:expr_2021),+ $(,)? } => {{
57        use $crate::alloc::borrow::ToOwned as _;
58
59        let mut map = $crate::alloc::collections::BTreeMap::new();
60        $(map.insert($key.to_owned(), $value.to_owned());)+
61        map
62    }};
63
64    { $($key:expr_2021 => $value:expr_2021),+ $(,)? } => {{
65        let mut map = $crate::alloc::collections::BTreeMap::new();
66        $(map.insert($key, $value);)+
67        map
68    }};
69}
70
71/// Creates an allocated `BTreeSet` from values.
72///
73/// Repeated values are stored once. The `owned:` form calls `to_owned()` on
74/// each value.
75///
76/// ```
77/// use collections::bset;
78///
79/// assert_eq!(bset![1, 2, 3, 1], bset![1, 2, 3]);
80/// let owned = bset! { owned: "one", "two" };
81/// assert!(owned.contains("one"));
82/// ```
83#[macro_export]
84macro_rules! bset {
85    {} => {
86        $crate::alloc::collections::BTreeSet::new()
87    };
88
89    { owned: $($value:expr_2021),+ $(,)? } => {{
90        use $crate::alloc::borrow::ToOwned as _;
91
92        let mut set = $crate::alloc::collections::BTreeSet::new();
93        $(set.insert($value.to_owned());)+
94        set
95    }};
96
97    { $($value:expr_2021),+ $(,)? } => {{
98        let mut set = $crate::alloc::collections::BTreeSet::new();
99        $(set.insert($value);)+
100        set
101    }};
102}
103
104/// Creates an allocated `LinkedList` from values, appending each value to the
105/// end of the list.
106///
107/// The `owned:` form calls `to_owned()` on each value.
108///
109/// ```
110/// use collections::list;
111///
112/// let items = list! { "first" => "second" };
113/// assert_eq!(items.len(), 2);
114/// let owned = list! { owned: "first" => "second" };
115/// assert_eq!(owned.front().unwrap(), "first");
116/// ```
117#[macro_export]
118macro_rules! list {
119    {} => {
120        $crate::alloc::collections::LinkedList::new()
121    };
122
123    { owned: $($value:expr_2021)=>+ } => {{
124        use $crate::alloc::borrow::ToOwned as _;
125
126        let mut list = $crate::alloc::collections::LinkedList::new();
127        $(list.push_back($value.to_owned());)+
128        list
129    }};
130
131    { $($value:expr_2021)=>+ } => {{
132        let mut list = $crate::alloc::collections::LinkedList::new();
133        $(list.push_back($value);)+
134        list
135    }};
136}
137
138/// Creates an allocated `VecDeque` from values, appending each value to the
139/// back of the deque.
140///
141/// The `owned:` form calls `to_owned()` on each value.
142///
143/// ```
144/// use collections::deque;
145///
146/// let items = deque! { "first" => "second" };
147/// assert_eq!(items.front(), Some(&"first"));
148/// let owned = deque! { owned: "first" => "second" };
149/// assert_eq!(owned.front().unwrap(), "first");
150/// ```
151#[macro_export]
152macro_rules! deque {
153    {} => {
154        $crate::alloc::collections::VecDeque::new()
155    };
156
157    { owned: $($value:expr_2021)=>+ } => {{
158        use $crate::alloc::borrow::ToOwned as _;
159
160        let mut deque = $crate::alloc::collections::VecDeque::with_capacity(
161            const { <[()]>::len(&[$({ ::core::stringify!($value); }),+]) },
162        );
163        $(deque.push_back($value.to_owned());)+
164        deque
165    }};
166
167    { $($value:expr_2021)=>+ } => {{
168        let mut deque = $crate::alloc::collections::VecDeque::with_capacity(
169            const { <[()]>::len(&[$({ ::core::stringify!($value); }),+]) },
170        );
171        $(deque.push_back($value);)+
172        deque
173    }};
174}
175
176/// Creates an allocated `BinaryHeap` from values.
177///
178/// The `owned:` form calls `to_owned()` on each value.
179///
180/// ```
181/// use collections::heap;
182///
183/// let items = heap![1, 3, 2];
184/// assert_eq!(items.peek(), Some(&3));
185/// let owned = heap! { owned: "first", "second" };
186/// assert_eq!(owned.peek().unwrap(), "second");
187/// ```
188#[macro_export]
189macro_rules! heap {
190    {} => {
191        $crate::alloc::collections::BinaryHeap::new()
192    };
193
194    { owned: $($value:expr_2021),+ $(,)? } => {{
195        use $crate::alloc::borrow::ToOwned as _;
196
197        let mut heap = $crate::alloc::collections::BinaryHeap::with_capacity(
198            const { <[()]>::len(&[$({ ::core::stringify!($value); }),+]) },
199        );
200        $(heap.push($value.to_owned());)+
201        heap
202    }};
203
204    { $($value:expr_2021),+ $(,)? } => {{
205        let mut heap = $crate::alloc::collections::BinaryHeap::with_capacity(
206            const { <[()]>::len(&[$({ ::core::stringify!($value); }),+]) },
207        );
208        $(heap.push($value);)+
209        heap
210    }};
211}
212
213/// Creates a `HashMap` from key-value pairs. Available with the `std` feature.
214///
215/// The `owned:` form calls `to_owned()` on each key and value.
216///
217/// ```
218/// use collections::map;
219///
220/// let map = map! { 1 => "one", 2 => "two" };
221/// assert_eq!(map.len(), 2);
222/// let owned = map! { owned: "key" => "value" };
223/// assert_eq!(owned["key"], "value");
224/// ```
225#[cfg(feature = "std")]
226#[macro_export]
227macro_rules! map {
228    {} => {
229        $crate::std::collections::HashMap::new()
230    };
231
232    { owned: $($key:expr_2021 => $value:expr_2021),+ $(,)? } => {{
233        use $crate::alloc::borrow::ToOwned as _;
234
235        let mut map = $crate::std::collections::HashMap::with_capacity(
236            const { <[()]>::len(&[$({ ::core::stringify!($key); }),+]) },
237        );
238        $(map.insert($key.to_owned(), $value.to_owned());)+
239        map
240    }};
241
242    { $($key:expr_2021 => $value:expr_2021),+ $(,)? } => {{
243        let mut map = $crate::std::collections::HashMap::with_capacity(
244            const { <[()]>::len(&[$({ ::core::stringify!($key); }),+]) },
245        );
246        $(map.insert($key, $value);)+
247        map
248    }};
249}
250
251/// Creates a `HashSet` from values. Available with the `std` feature.
252///
253/// Repeated values are stored once. The `owned:` form calls `to_owned()` on
254/// each value.
255///
256/// ```
257/// use collections::set;
258///
259/// assert_eq!(set![1, 2, 3, 1], set![1, 2, 3]);
260/// let owned = set! { owned: "one", "two" };
261/// assert!(owned.contains("one"));
262/// ```
263#[cfg(feature = "std")]
264#[macro_export]
265macro_rules! set {
266    {} => {
267        $crate::std::collections::HashSet::new()
268    };
269
270    { owned: $($value:expr_2021),+ $(,)? } => {{
271        use $crate::alloc::borrow::ToOwned as _;
272
273        let mut set = $crate::std::collections::HashSet::with_capacity(
274            const { <[()]>::len(&[$({ ::core::stringify!($value); }),+]) },
275        );
276        $(set.insert($value.to_owned());)+
277        set
278    }};
279
280    { $($value:expr_2021),+ $(,)? } => {{
281        let mut set = $crate::std::collections::HashSet::with_capacity(
282            const { <[()]>::len(&[$({ ::core::stringify!($value); }),+]) },
283        );
284        $(set.insert($value);)+
285        set
286    }};
287}