Skip to content

Commit 4c6c989

Browse files
committed
rust: linked_list: add usage example
Illustrate how to use the linked list adding an example that uses all methods the linked list currently supports. Link: Rust-for-Linux#345 Reviewed-by: David Gow <[email protected]> Signed-off-by: José Expósito <[email protected]>
1 parent bd12347 commit 4c6c989

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

rust/kernel/linked_list.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,84 @@ impl<T: GetLinks + ?Sized> GetLinks for Arc<T> {
109109
///
110110
/// Elements in the list are wrapped and ownership is transferred to the list while the element is
111111
/// in the list.
112+
///
113+
/// # Examples
114+
///
115+
/// In the example below we create a list of numbers, implement `GetLinks` for
116+
/// it and perform several operations on the list:
117+
///
118+
/// ```
119+
/// use kernel::linked_list::{GetLinks, Links, List, Wrapper};
120+
/// use kernel::sync::Arc;
121+
///
122+
/// struct NumList {
123+
/// num: i32,
124+
/// links: Links<NumList>,
125+
/// }
126+
///
127+
/// // We need to implement GetLinks for our list indicating:
128+
/// impl GetLinks for NumList {
129+
/// // The type of the entries of the list:
130+
/// type EntryType = Self;
131+
/// // And how to get the link to the previous and next items:
132+
/// fn get_links(data: &Self) -> &Links<Self> {
133+
/// &data.links
134+
/// }
135+
/// }
136+
///
137+
/// let first_item = Arc::try_new(NumList {
138+
/// num: 1,
139+
/// links: Links::new(),
140+
/// })?;
141+
///
142+
/// let second_item = Arc::try_new(NumList {
143+
/// num: 2,
144+
/// links: Links::new(),
145+
/// })?;
146+
///
147+
/// let third_item = Arc::try_new(NumList {
148+
/// num: 3,
149+
/// links: Links::new(),
150+
/// })?;
151+
///
152+
/// let mut list: List<Arc<NumList>> = List::new();
153+
/// assert!(list.is_empty());
154+
///
155+
/// list.push_back(first_item.clone());
156+
/// assert!(!list.is_empty());
157+
///
158+
/// // Pushing the same item twice has no effect
159+
/// list.push_back(first_item.clone());
160+
///
161+
/// unsafe { list.remove(&first_item) };
162+
/// assert!(list.is_empty());
163+
///
164+
/// list.push_back(second_item.clone());
165+
/// assert!(!list.is_empty());
166+
///
167+
/// unsafe { list.insert_after(second_item.into_pointer(), third_item) };
168+
///
169+
/// // At this point `second_item` and `third_item` are in the list with values
170+
/// // 2 and 3. Decrease their values by 1.
171+
/// let mut cursor = list.cursor_front_mut();
172+
/// while let Some(item) = cursor.current() {
173+
/// item.num -= 1;
174+
/// cursor.move_next();
175+
/// }
176+
///
177+
/// let item = list.pop_front().unwrap();
178+
/// assert_eq!(1, item.num);
179+
///
180+
/// let item = list.pop_front().unwrap();
181+
/// assert_eq!(2, item.num);
182+
///
183+
/// assert!(list.is_empty());
184+
///
185+
/// let item = list.pop_front();
186+
/// assert!(item.is_none());
187+
///
188+
/// # Ok::<(), Error>(())
189+
/// ```
112190
pub struct List<G: GetLinksWrapped> {
113191
list: RawList<G>,
114192
}

0 commit comments

Comments
 (0)