Skip to content

Commit 70cee0d

Browse files
committed
📝 adjust some functions
1 parent fb90471 commit 70cee0d

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

src/link.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use alloc::sync::{Arc, Weak};
21
use core::fmt;
32
use core::marker::PhantomData;
43
use core::sync::atomic::{AtomicUsize, Ordering};
@@ -12,10 +11,6 @@ const REFCOUNT_MASK: usize = (1 << (LEADING_BITS + ALIGN_BITS)) - 1;
1211
const UPDTATE_MASK: usize = 1 << (LEADING_BITS + ALIGN_BITS - 1);
1312
const UPDATE_REF_MASK: usize = REFCOUNT_MASK & !UPDTATE_MASK;
1413

15-
//---------------------------------------------------------------------------------------
16-
// LinkWrapper
17-
//---------------------------------------------------------------------------------------
18-
1914
#[repr(C)]
2015
union Ptr<T> {
2116
addr: usize,
@@ -153,17 +148,3 @@ impl<T: fmt::Debug> fmt::Debug for LinkWrapper<T> {
153148
f.debug_struct("Link").field("ptr", &ptr).finish()
154149
}
155150
}
156-
157-
#[inline]
158-
pub(crate) fn ptr_to_arc<T>(ptr: *const T) -> Option<Arc<T>> {
159-
(!ptr.is_null()).then(|| unsafe { Arc::from_raw(ptr) })
160-
}
161-
162-
#[inline]
163-
pub(crate) fn ptr_to_weak<T>(ptr: *const T) -> Weak<T> {
164-
if ptr.is_null() {
165-
Weak::new()
166-
} else {
167-
unsafe { Weak::from_raw(ptr) }
168-
}
169-
}

src/rcu_cell.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use core::mem::ManuallyDrop;
33
use core::ptr;
44
use core::sync::atomic::Ordering;
55

6-
use crate::link::{ptr_to_arc, LinkWrapper};
6+
use crate::link::LinkWrapper;
77

8-
//---------------------------------------------------------------------------------------
9-
// RcuCell
10-
//---------------------------------------------------------------------------------------
8+
#[inline]
9+
fn ptr_to_arc<T>(ptr: *const T) -> Option<Arc<T>> {
10+
(!ptr.is_null()).then(|| unsafe { Arc::from_raw(ptr) })
11+
}
1112

1213
/// RCU cell, it behaves like `RwLock<Option<Arc<T>>>`
1314
#[derive(Debug)]

src/rcu_weak.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ use core::mem::ManuallyDrop;
33
use core::ptr;
44
use core::sync::atomic::Ordering;
55

6-
use crate::link::{ptr_to_weak, LinkWrapper};
7-
//---------------------------------------------------------------------------------------
8-
// RcuWeak
9-
//---------------------------------------------------------------------------------------
6+
use crate::link::LinkWrapper;
7+
8+
#[inline]
9+
fn ptr_to_weak<T>(ptr: *const T) -> Weak<T> {
10+
if ptr.is_null() {
11+
Weak::new()
12+
} else {
13+
unsafe { Weak::from_raw(ptr) }
14+
}
15+
}
1016

1117
/// RCU weak cell, it behaves like `RwLock<Weak<T>>`
1218
#[derive(Debug)]
@@ -48,6 +54,15 @@ impl<T> RcuWeak<T> {
4854
}
4955
}
5056

57+
/// convert the rcu weak to a `Weak`` value
58+
#[inline]
59+
pub fn into_weak(self) -> Weak<T> {
60+
let ptr = self.link.get_ref();
61+
let ret = ptr_to_weak(ptr);
62+
let _ = ManuallyDrop::new(self);
63+
ret
64+
}
65+
5166
/// write a new weak value to the rcu weak cell and return the old value
5267
#[inline]
5368
pub fn write(&self, data: Weak<T>) -> Weak<T> {

0 commit comments

Comments
 (0)