collection_tools/collection/vec_deque.rs
1#[ allow( unused_imports, clippy::wildcard_imports ) ]
2use super::*;
3
4#[ doc( inline ) ]
5#[ allow( unused_imports ) ]
6#[ allow( clippy::pub_use ) ]
7pub use alloc::collections::vec_deque::*;
8
9/// Creates a `VecDeque` from a list of elements.
10///
11/// The `deque` macro allows for the convenient creation of a `VecDeque` with initial elements.
12/// Elements passed to the macro are automatically converted into the deque's element type
13/// using `.into()`, enabling the use of literals or values of different, but convertible types.
14///
15/// Note: The `deque` macro relies on the `.into()` method to convert each element into the target type
16/// of the `VecDeque`. This means that the elements must be compatible with the `Into<T>` trait for the
17/// type `T` used in the `VecDeque`.
18///
19/// # Origin
20///
21/// This collection is reexported from `alloc`.
22///
23/// # Syntax
24///
25/// The macro can be called with a comma-separated list of elements. A trailing comma is optional.
26///
27/// ```rust
28/// # use collection_tools::{ VecDeque, deque };
29/// // VecDeque of i32
30/// let vd1 = deque!( 1, 2, 3, 4, 5 );
31///
32/// // VecDeque of String
33/// let vd2 = deque!{ "hello", "world", "rust" };
34///
35/// // With trailing comma
36/// let vd3 = deque!( 1.1, 2.2, 3.3, );
37/// ```
38///
39/// # Parameters
40///
41/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `VecDeque`.
42/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the
43/// type stored in the `VecDeque`.
44///
45/// # Returns
46///
47/// Returns a `VecDeque` containing all the specified elements. The capacity of the deque is
48/// automatically determined based on the number of elements provided.
49///
50/// # Example
51///
52/// Basic usage with integers:
53///
54/// ```rust
55/// # use collection_tools::{ VecDeque, deque };
56/// let vd : VecDeque< i32 > = deque!( 1, 2, 3 );
57/// assert_eq!( vd.front(), Some( &1 ) ); // The first element is 1
58/// assert_eq!( vd.back(), Some( &3 ) ); // The last element is 3
59/// ```
60///
61/// # Example
62///
63/// Creating a `VecDeque` of `&str` from string literals:
64///
65/// ```rust
66/// # use collection_tools::{ VecDeque, deque };
67/// let fruits = deque!{ "apple", "banana", "cherry" };
68/// assert_eq!( fruits.front(), Some( &"apple" ) ); // The first element
69/// assert_eq!( fruits.back(), Some( &"cherry" ) ); // The last element
70/// ```
71///
72#[ cfg( feature = "collection_constructors" ) ]
73#[ macro_export( local_inner_macros ) ]
74macro_rules! deque
75{
76 (
77 $( $key : expr ),* $( , )?
78 )
79 =>
80 {{
81 let _cap = count!( @count $( $key ),* );
82 let mut _vecd = $crate::collection::VecDeque::with_capacity( _cap );
83 $(
84 _vecd.push_back( $key );
85 )*
86 _vecd
87 }};
88}
89
90/// Creates a `VecDeque` from a list of elements.
91///
92/// The `into_vecd` macro allows for the convenient creation of a `VecDeque` with initial elements.
93/// Elements passed to the macro are automatically converted into the deque's element type
94/// using `.into()`, enabling the use of literals or values of different, but convertible types.
95///
96/// Note: The `into_vecd` macro relies on the `.into()` method to convert each element into the target type
97/// of the `VecDeque`. This means that the elements must be compatible with the `Into<T>` trait for the
98/// type `T` used in the `VecDeque`.
99///
100/// # Origin
101///
102/// This collection is reexported from `alloc`.
103///
104/// # Syntax
105///
106/// The macro can be called with a comma-separated list of elements. A trailing comma is optional.
107///
108/// ```rust
109/// # use collection_tools::{ VecDeque, into_vecd };
110/// // VecDeque of i32
111/// let vd1 : VecDeque< i32 > = into_vecd!( 1, 2, 3, 4, 5 );
112///
113/// // VecDeque of String
114/// let vd2 : VecDeque< String > = into_vecd!{ "hello".to_string(), "world", "rust" };
115///
116/// // With trailing comma
117/// let vd3 : VecDeque< f64 > = into_vecd!( 1.1, 2.2, 3.3, );
118/// ```
119///
120/// # Parameters
121///
122/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `VecDeque`.
123/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the
124/// type stored in the `VecDeque`.
125///
126/// # Returns
127///
128/// Returns a `VecDeque` containing all the specified elements. The capacity of the deque is
129/// automatically determined based on the number of elements provided.
130///
131/// # Example
132///
133/// Basic usage with integers:
134///
135/// ```rust
136/// # use collection_tools::{ VecDeque, into_vecd };
137/// let vd : VecDeque< i32 > = into_vecd!( 1, 2, 3 );
138/// assert_eq!( vd.front(), Some( &1 ) ); // The first element is 1
139/// assert_eq!( vd.back(), Some( &3 ) ); // The last element is 3
140/// ```
141///
142/// # Example
143///
144/// Using with different types that implement `Into< T >`:
145///
146/// ```rust
147/// # use collection_tools::{ VecDeque, into_vecd };
148/// let chars : VecDeque< char > = into_vecd!( 'a', 'b', 'c' );
149/// assert!( chars.contains( &'a' ) );
150/// assert!( chars.contains( &'b' ) );
151/// assert!( chars.contains( &'c' ) );
152/// ```
153///
154/// # Example
155///
156/// Creating a `VecDeque` of `String` from string literals:
157///
158/// ```rust
159/// # use collection_tools::{ VecDeque, into_vecd };
160/// let fruits : VecDeque< String > = into_vecd!{ "apple", "banana", "cherry" };
161/// assert_eq!( fruits.front(), Some( &"apple".to_string() ) ); // The first element
162/// assert_eq!( fruits.back(), Some( &"cherry".to_string() ) ); // The last element
163/// ```
164///
165#[ cfg( feature = "collection_into_constructors" ) ]
166#[ macro_export( local_inner_macros ) ]
167macro_rules! into_vecd
168{
169 (
170 $( $key : expr ),* $( , )?
171 )
172 =>
173 {{
174 let _cap = count!( @count $( $key ),* );
175 let mut _vecd = $crate::collection::VecDeque::with_capacity( _cap );
176 $(
177 _vecd.push_back( Into::into( $key ) );
178 )*
179 _vecd
180 }};
181}