feat: utils: linked_list: do not require clone trait

This commit is contained in:
Paul Pan 2024-05-05 00:16:19 +08:00
parent 1ce03e09cc
commit d2155ed7cf
2 changed files with 18 additions and 4 deletions

View File

@ -35,7 +35,6 @@ impl RawCap {
} }
} }
#[derive(Clone)]
pub struct CapEntry { pub struct CapEntry {
pub cap: Cell<RawCap>, pub cap: Cell<RawCap>,
pub link: Link<Self>, pub link: Link<Self>,
@ -59,6 +58,14 @@ impl CapEntry {
} }
} }
impl Clone for CapEntry {
fn clone(&self) -> Self {
let mut cte = Self::new(self.cap.get());
cte.link = self.link.clone();
cte
}
}
impl From<RawCap> for CapEntry { impl From<RawCap> for CapEntry {
fn from(value: RawCap) -> Self { fn from(value: RawCap) -> Self {
Self::new(value) Self::new(value)

View File

@ -10,7 +10,7 @@ macro_rules! LinkHelperImpl {
}; };
} }
pub trait LinkHelper: Clone { pub trait LinkHelper: Sized {
const LINK_OFFSET: usize; const LINK_OFFSET: usize;
/// # Safety /// # Safety
@ -20,7 +20,6 @@ pub trait LinkHelper: Clone {
} }
} }
#[derive(Clone)]
pub struct Link<T: LinkHelper> { pub struct Link<T: LinkHelper> {
pub prev: Cell<Option<NonNull<T>>>, pub prev: Cell<Option<NonNull<T>>>,
pub next: Cell<Option<NonNull<T>>>, pub next: Cell<Option<NonNull<T>>>,
@ -35,6 +34,15 @@ impl<T: LinkHelper> Default for Link<T> {
} }
} }
impl<T: LinkHelper> Clone for Link<T> {
fn clone(&self) -> Self {
Self {
prev: Cell::new(self.prev.get()),
next: Cell::new(self.next.get()),
}
}
}
impl<T: LinkHelper> Link<T> { impl<T: LinkHelper> Link<T> {
/// # Safety /// # Safety
/// LINK_OFFSET must be a valid field offset of T /// LINK_OFFSET must be a valid field offset of T
@ -113,7 +121,6 @@ impl<T: LinkHelper> Link<T> {
mod tests { mod tests {
use super::*; use super::*;
#[derive(Clone)]
struct Node { struct Node {
link: Link<Node>, link: Link<Node>,
value: i32, value: i32,