Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guoweikang/linked list #1

Merged
merged 25 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
afc15de
Synchronize r4l linked_lists latest code
guoweikang Oct 14, 2024
db2898d
Update LinkedList to make it easier to use
guoweikang Oct 15, 2024
16b3073
remove unused unsafe_list source code
guoweikang Oct 15, 2024
3d826d9
add macro def_node and def_generic_node
guoweikang Oct 15, 2024
5a3f0c4
fix clippy warnning
guoweikang Oct 15, 2024
b8fb68b
fix doc warning
guoweikang Oct 15, 2024
aa5cb4c
add inner() func for node
guoweikang Oct 15, 2024
6f38819
create tag and update version
guoweikang Oct 15, 2024
e01115f
add doc for pub func
guoweikang Oct 16, 2024
d5641a7
inner not a pub filed, use inner() api access
guoweikang Oct 16, 2024
deaebf9
Add DOC and example for def_node macro
guoweikang Oct 17, 2024
5d853ff
use const fn for inner and is_empty
guoweikang Oct 18, 2024
02e5d46
macro def_node with new format
guoweikang Oct 21, 2024
58a879b
remove unsed description
guoweikang Oct 21, 2024
606187d
support pub(crate) for def_node
guoweikang Oct 21, 2024
33d763c
use const for more func
guoweikang Oct 21, 2024
c2c3542
use vis in macro
guoweikang Oct 21, 2024
d66aa7e
Add CI DOC for linked list
guoweikang Oct 22, 2024
907953b
modify crates-io package name from linked_list to linked_list_r4l
guoweikang Oct 22, 2024
0cc8710
fix cargo fmt
guoweikang Oct 22, 2024
f714731
fix unitest fail
guoweikang Oct 22, 2024
d289a96
update package version to 0.2.0
guoweikang Oct 22, 2024
3509ccf
use vis in _def_node_inernal
guoweikang Oct 22, 2024
325bf0a
rename repo to linked_list_r4l
guoweikang Oct 22, 2024
793fdcc
remove const fn for into_innner
guoweikang Oct 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "linked_list"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Wedson Almeida Filho <[email protected]>"]
authors = ["Wedson Almeida Filho <[email protected]>", "WeiKang Guo <[email protected]>"]
description = "Linked lists that supports arbitrary removal in constant time"
license = "GPL-2.0-or-later"
homepage = "https://github.com/arceos-org/arceos"
Expand Down
201 changes: 197 additions & 4 deletions src/lib.rs
guoweikang marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,205 @@
//! It is based on the linked list implementation in [Rust-for-Linux][1].
//!
//! [1]: https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/linked_list.rs
//!
//! In more general use cases, shoud not use RawList directly,
guoweikang marked this conversation as resolved.
Show resolved Hide resolved
//! suggest use smart pointers of nodes and move ownership of smart
//! pointers to List:
//!
//! ```
//! use linked_list::{GetLinks, Links, List};
//!
//! type InnerType = usize;
//!
//! pub struct ExampleNode {
//! pub inner: InnerType,
//! links: Links<Self>,
//! }
//!
//! impl GetLinks for ExampleNode {
//! type EntryType = Self;
//!
//! fn get_links(t: &Self) -> &Links<Self> {
//! &t.links
//! }
//! }
//!
//! impl ExampleNode {
//! fn new(inner: InnerType) -> Self {
//! Self {
//! inner,
//! links: Links::new()
//! }
//! }
//!
//! fn inner(&self) -> &InnerType {
//! &self.inner
//! }
//! }
//!
//! let node1 = Box::new(ExampleNode::new(0));
//! let node2 = Box::new(ExampleNode::new(1));
//! let mut list = List::<Box<ExampleNode>>::new();
//!
//! list.push_back(node1);
//! list.push_back(node2);
//!
//! //Support Iter
//! for (i,e) in list.iter().enumerate() {
//! assert!(*e.inner() == i);
//! }
//!
//! // Pop drop
//! assert!(*list.pop_front().unwrap().inner() == 0);
//! assert!(*list.pop_front().unwrap().inner() == 1);
//!
//! ```
//!

#![no_std]
#![cfg_attr(not(test), no_std)]

mod linked_list;
mod raw_list;
pub use linked_list::List;
pub use raw_list::{GetLinks, Links};

/// Defines a new node type that wraps an inner type and includes links for List.
///
/// # Parameters
///
/// - `struct_name`: The name of the struct to define.
/// - `type`: The inner type to wrap.
///
/// # Example
///
/// ```rust
/// use linked_list::{def_node, List};
///
/// def_node!(ExampleNode, usize);
///
/// let node1 = Box::new(ExampleNode::new(0));
/// let node2 = Box::new(ExampleNode::new(1));
/// let mut list = List::<Box<ExampleNode>>::new();
///
/// list.push_back(node1);
/// list.push_back(node2);
///
/// for (i,e) in list.iter().enumerate() {
/// assert!(*e.inner() == i);
/// }
/// ```
#[macro_export]
macro_rules! def_node {
equation314 marked this conversation as resolved.
Show resolved Hide resolved
($struct_name:ident, $type:ty) => {
#[doc = "A node wrapper for inner type "]
guoweikang marked this conversation as resolved.
Show resolved Hide resolved
pub struct $struct_name {
inner: $type,
links: $crate::Links<Self>,
}

impl $crate::GetLinks for $struct_name {
type EntryType = Self;

#[inline]
fn get_links(t: &Self) -> &$crate::Links<Self> {
&t.links
}
}

impl $struct_name {
#[doc = "Create a node"]
pub const fn new(inner: $type) -> Self {
Self {
inner,
links: $crate::Links::new(),
}
}

#[inline]
#[doc = "Get inner"]
pub const fn inner(&self) -> &$type {
&self.inner
}
}
guoweikang marked this conversation as resolved.
Show resolved Hide resolved

impl core::ops::Deref for $struct_name {
type Target = $type;

#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
};
}

/// Defines a generic node type that wraps a generic inner type and includes links
/// for List.
///
/// Similar to [`def_node`], but inner type is generic Type
///
/// # Parameters
///
/// - `struct_name`: The name of the struct to define.
///
/// # Example
///
/// ```rust
/// use linked_list::{def_generic_node, List};
/// def_generic_node!(GenericExampleNode);
///
/// let node1 = Box::new(GenericExampleNode::new(0));
/// let node2 = Box::new(GenericExampleNode::new(1));
/// let mut list = List::<Box<GenericExampleNode<usize>>>::new();
///
/// list.push_back(node1);
/// list.push_back(node2);
///
/// for (i,e) in list.iter().enumerate() {
/// assert!(*e.inner() == i);
/// }
/// ```
#[macro_export]
macro_rules! def_generic_node {
($struct_name:ident) => {
#[doc = "A node wrapper include a generic type"]
pub struct $struct_name<T> {
inner: T,
links: $crate::Links<Self>,
}

impl<T> $crate::GetLinks for $struct_name<T> {
type EntryType = Self;

#[inline]
fn get_links(t: &Self) -> &$crate::Links<Self> {
&t.links
}
}

impl<T> $struct_name<T> {
#[doc = "Create a node"]
pub const fn new(inner: T) -> Self {
Self {
inner,
links: $crate::Links::new(),
}
}

#[inline]
#[doc = "Get inner"]
pub const fn inner(&self) -> &T {
&self.inner
}
}

pub mod unsafe_list;
impl<T> core::ops::Deref for $struct_name<T> {
type Target = T;

pub use self::linked_list::{AdapterWrapped, List, Wrapper};
pub use unsafe_list::{Adapter, Cursor, Links};
#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
};
}
Loading