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

Fix layout differences of Header<Atomic> v.s. Header<NonAtomic> #59

Merged
merged 1 commit into from
Feb 10, 2022
Merged
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
29 changes: 18 additions & 11 deletions src/tendril.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,25 @@ pub unsafe trait Atomicity: 'static {
///
/// This is akin to using `Rc` for reference counting.
#[repr(C)]
pub struct NonAtomic(Cell<PackedUsize>);

#[repr(C, packed)]
#[derive(Copy, Clone)]
struct PackedUsize(usize);
pub struct NonAtomic(Cell<usize>);

unsafe impl Atomicity for NonAtomic {
#[inline]
fn new() -> Self {
NonAtomic(Cell::new(PackedUsize(1)))
NonAtomic(Cell::new(1))
}

#[inline]
fn increment(&self) -> usize {
let value = self.0.get().0;
self.0.set(PackedUsize(value.checked_add(1).expect(OFLOW)));
let value = self.0.get();
self.0.set(value.checked_add(1).expect(OFLOW));
value
}

#[inline]
fn decrement(&self) -> usize {
let value = self.0.get().0;
self.0.set(PackedUsize(value - 1));
let value = self.0.get();
self.0.set(value - 1);
value
}

Expand Down Expand Up @@ -130,6 +126,7 @@ unsafe impl Atomicity for Atomic {
}
}

#[repr(C)] // Preserve field order for cross-atomicity transmutes
struct Header<A: Atomicity> {
refcount: A,
cap: u32,
Expand Down Expand Up @@ -1712,7 +1709,7 @@ mod test {
mem::size_of::<Header<Atomic>>(),
);
assert_eq!(
mem::size_of::<*const ()>() + 4,
mem::size_of::<Header<Atomic>>(),
mem::size_of::<Header<NonAtomic>>(),
);
}
Expand Down Expand Up @@ -2448,6 +2445,16 @@ mod test {
assert_eq!("this is a string", &*t);
}

/// https://github.com/servo/tendril/issues/58
#[test]
fn issue_58() {
let data = "<p><i>Hello!</p>, World!</i>";
let s: Tendril<fmt::UTF8, NonAtomic> = data.into();
assert_eq!(&*s, data);
let s: Tendril<fmt::UTF8, Atomic> = s.into_send().into();
assert_eq!(&*s, data);
}

#[test]
fn inline_send() {
let s = "x".to_tendril();
Expand Down