Skip to content
Open
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
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::env;

fn main() {
println!("cargo:rustc-check-cfg=cfg(bytes_has_atomic_ptr)");

let target_atomics = env::var("CARGO_CFG_TARGET_HAS_ATOMIC").unwrap_or_default();
if target_atomics.split(',').any(|atomic| atomic == "ptr") {
println!("cargo:rustc-cfg=bytes_has_atomic_ptr");
}
}
10 changes: 10 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use alloc::{
vec::Vec,
};

#[cfg(bytes_has_atomic_ptr)]
use alloc::sync::Arc;

use crate::buf::IntoIter;
#[allow(unused)]
use crate::loom::sync::atomic::AtomicMut;
Expand Down Expand Up @@ -1014,6 +1017,13 @@ impl From<Box<[u8]>> for Bytes {
}
}

#[cfg(bytes_has_atomic_ptr)]
impl From<Arc<[u8]>> for Bytes {
fn from(arc: Arc<[u8]>) -> Bytes {
Bytes::from_owner(arc)
}
}

impl From<Bytes> for BytesMut {
/// Convert self into `BytesMut`.
///
Expand Down
12 changes: 12 additions & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ fn from_slice() {
assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
}

#[cfg(bytes_has_atomic_ptr)]
#[test]
Comment thread
martin-g marked this conversation as resolved.
Resolved
fn from_arc() {
let source: Arc<[u8]> = Arc::from(&b"abcdefgh"[..]);
let source_ptr = source.as_ptr();
let bytes = Bytes::from(source.clone());
drop(source);

assert_eq!(bytes, b"abcdefgh"[..]);
assert_eq!(bytes.as_ptr(), source_ptr);
}

#[test]
fn fmt() {
let a = format!("{:?}", Bytes::from(&b"abcdefg"[..]));
Expand Down
Loading