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

Add safety sections, fix clippy warnings #73

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 12 additions & 4 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl<T> Arc<T> {
/// by the atomic count.
///
/// It is recommended to use OffsetArc for this
///
/// # Safety
/// - The given pointer must be a valid pointer to `T` that came from [`Arc::into_raw`].
/// - After `from_raw`, the pointer must not be accessed.
#[inline]
pub unsafe fn from_raw(ptr: *const T) -> Self {
// FIXME: when `byte_sub` is stabilized, this can accept T: ?Sized.
Expand Down Expand Up @@ -151,6 +155,10 @@ impl<T> Arc<[T]> {
/// [`Arc::from_raw`] should accept unsized types, but this is not trivial to do correctly
/// until the feature [`pointer_bytes_offsets`](https://github.com/rust-lang/rust/issues/96283)
/// is stabilized. This is stopgap solution for slices.
///
/// # Safety
/// - The given pointer must be a valid pointer to `[T]` that came from [`Arc::into_raw`].
/// - After `from_raw_slice`, the pointer must not be accessed.
pub unsafe fn from_raw_slice(ptr: *const [T]) -> Self {
let len = (*ptr).len();
// Assuming the offset of `T` in `ArcInner<T>` is the same
Expand Down Expand Up @@ -471,7 +479,7 @@ impl<T: Clone> Arc<T> {
pub fn make_mut(this: &mut Self) -> &mut T {
if !this.is_unique() {
// Another pointer exists; clone
*this = Arc::new(T::clone(&this));
*this = Arc::new(T::clone(this));
}

unsafe {
Expand All @@ -497,7 +505,7 @@ impl<T: Clone> Arc<T> {
pub fn make_unique(this: &mut Self) -> &mut UniqueArc<T> {
if !this.is_unique() {
// Another pointer exists; clone
*this = Arc::new(T::clone(&this));
*this = Arc::new(T::clone(this));
}

unsafe {
Expand Down Expand Up @@ -712,14 +720,14 @@ impl<A> FromIterator<A> for Arc<[A]> {
impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
#[inline]
fn borrow(&self) -> &T {
&**self
self
}
}

impl<T: ?Sized> AsRef<T> for Arc<T> {
#[inline]
fn as_ref(&self) -> &T {
&**self
self
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/arc_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl<'a, T> ArcBorrow<'a, T> {
/// e.g. if we obtain such a reference over FFI
/// TODO: should from_ref be relaxed to unsized types? It can't be
/// converted back to an Arc right now for unsized types.
/// # Safety
/// - The reference to `T` must have come from a Triomphe Arc, UniqueArc, or ArcBorrow.
#[inline]
pub unsafe fn from_ref(r: &'a T) -> Self {
ArcBorrow(r)
Expand Down
4 changes: 2 additions & 2 deletions src/unique_arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<T: ?Sized> UniqueArc<T> {
///
/// The given `Arc` must have a reference count of exactly one
pub(crate) unsafe fn from_arc_ref(arc: &mut Arc<T>) -> &mut Self {
debug_assert_eq!(Arc::count(&arc), 1);
debug_assert_eq!(Arc::count(arc), 1);

// Safety: caller guarantees that `arc` is unique,
// `UniqueArc` is `repr(transparent)`
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<T: ?Sized> Deref for UniqueArc<T> {

#[inline]
fn deref(&self) -> &T {
&*self.0
&self.0
}
}

Expand Down
Loading