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

Conditionally compile in Arc #140

Merged
merged 1 commit into from
Nov 5, 2022
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
9 changes: 8 additions & 1 deletion src/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
//! ["extern_crate_alloc"]}`

use super::*;
#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;
use alloc::{
alloc::{alloc_zeroed, Layout},
boxed::Box,
rc::Rc,
sync::Arc,
vec,
vec::Vec,
};
Expand Down Expand Up @@ -355,6 +356,7 @@ pub fn try_cast_rc<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(

/// As [`try_cast_arc`](try_cast_arc), but unwraps for you.
#[inline]
#[cfg(target_has_atomic = "ptr")]
pub fn cast_arc<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(
input: Arc<A>,
) -> Arc<B> {
Expand All @@ -375,6 +377,7 @@ pub fn cast_arc<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(
/// alignment.
/// * The start and end size of the `Arc` must have the exact same size.
#[inline]
#[cfg(target_has_atomic = "ptr")]
pub fn try_cast_arc<
A: NoUninit + AnyBitPattern,
B: NoUninit + AnyBitPattern,
Expand Down Expand Up @@ -456,6 +459,7 @@ pub fn try_cast_slice_rc<

/// As [`try_cast_slice_arc`](try_cast_slice_arc), but unwraps for you.
#[inline]
#[cfg(target_has_atomic = "ptr")]
pub fn cast_slice_arc<
A: NoUninit + AnyBitPattern,
B: NoUninit + AnyBitPattern,
Expand All @@ -480,6 +484,7 @@ pub fn cast_slice_arc<
/// * The start and end content size in bytes of the `Arc<[T]>` must be the
/// exact same.
#[inline]
#[cfg(target_has_atomic = "ptr")]
pub fn try_cast_slice_arc<
A: NoUninit + AnyBitPattern,
B: NoUninit + AnyBitPattern,
Expand Down Expand Up @@ -590,6 +595,7 @@ pub trait TransparentWrapperAlloc<Inner: ?Sized>:
/// Convert an [`Arc`](alloc::sync::Arc) to the inner type into an `Arc` to
/// the wrapper type.
#[inline]
#[cfg(target_has_atomic = "ptr")]
fn wrap_arc(s: Arc<Inner>) -> Arc<Self> {
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());

Expand Down Expand Up @@ -679,6 +685,7 @@ pub trait TransparentWrapperAlloc<Inner: ?Sized>:
/// Convert an [`Arc`](alloc::sync::Arc) to the wrapper type into an `Arc` to
/// the inner type.
#[inline]
#[cfg(target_has_atomic = "ptr")]
fn peel_arc(s: Arc<Self>) -> Arc<Inner> {
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());

Expand Down
17 changes: 11 additions & 6 deletions tests/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn test_transparent_wrapper() {
#[cfg(feature = "extern_crate_alloc")]
{
use bytemuck::allocation::TransparentWrapperAlloc;
use std::{rc::Rc, sync::Arc};
use std::rc::Rc;

let a: Vec<Foreign> = vec![Foreign::default(); 2];

Expand All @@ -101,11 +101,16 @@ fn test_transparent_wrapper() {
let i: Rc<Foreign> = Wrapper::peel_rc(h);
assert_eq!(&*i, &0);

let j: Arc<Foreign> = Arc::new(Foreign::default());
#[cfg(target_has_atomic = "ptr")]
{
use std::sync::Arc;

let k: Arc<Wrapper> = Wrapper::wrap_arc(j);
assert_eq!(&*k, &0);
let l: Arc<Foreign> = Wrapper::peel_arc(k);
assert_eq!(&*l, &0);
let j: Arc<Foreign> = Arc::new(Foreign::default());

let k: Arc<Wrapper> = Wrapper::wrap_arc(j);
assert_eq!(&*k, &0);
let l: Arc<Foreign> = Wrapper::peel_arc(k);
assert_eq!(&*l, &0);
}
}
}