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