Skip to content

Commit

Permalink
ci: use cargo hack
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo committed Jul 12, 2024
1 parent 10fcc8b commit a081710
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 34 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ jobs:
- uses: actions/checkout@v3
- name: rustfmt
run: cargo fmt -- --config "unstable_features=true,imports_granularity=Crate,group_imports=StdExternalCrate,format_code_in_doc_comments=true"
- name: clippy no-default-feature
run: cargo clippy --no-default-features -- -D warnings
- name: clippy all-features
run: cargo clippy --all-features --tests -- -D warnings
- uses: taiki-e/install-action@cargo-hack
- name: clippy
run: cargo hack clippy --feature-powerset -- -D warnings
- name: test
run: cargo test --all-features
- name: install miri
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repository = "https://github.com/wyfo/swap-buffer-queue"
default = ["std"]
alloc = []
std = ["alloc"]
stream = ["dep:futures-core", "dep:futures-util"]
stream = ["std", "dep:futures-core", "dep:futures-util"]
write = []

[dependencies]
Expand Down
16 changes: 9 additions & 7 deletions src/loom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ mod without_loom {
pub(crate) const BACKOFF_LIMIT: usize = 6;

#[derive(Debug, Default)]
pub(crate) struct LoomUnsafeCell<T>(cell::UnsafeCell<T>);

impl<T> LoomUnsafeCell<T> {
#[cfg(feature = "std")]
pub(crate) fn new(data: T) -> Self {
Self(cell::UnsafeCell::new(data))
}
pub(crate) struct LoomUnsafeCell<T: ?Sized>(cell::UnsafeCell<T>);

impl<T: ?Sized> LoomUnsafeCell<T> {
pub(crate) fn with<R>(&self, f: impl FnOnce(*const T) -> R) -> R {
f(self.0.get())
}
Expand All @@ -26,6 +21,13 @@ mod without_loom {
f(self.0.get())
}
}

#[cfg(feature = "std")]
impl<T> LoomUnsafeCell<T> {
pub(crate) fn new(data: T) -> Self {
Self(cell::UnsafeCell::new(data))
}
}
}

#[cfg(not(all(loom, test)))]
Expand Down
2 changes: 1 addition & 1 deletion src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! assert_eq!(writer.write(slice.frame()).unwrap(), 300);
//! ```
use std::ops::{Deref, DerefMut};
use core::ops::{Deref, DerefMut};

mod array;
#[cfg(feature = "alloc")]
Expand Down
12 changes: 5 additions & 7 deletions src/write/array.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::{
cell::{Cell, UnsafeCell},
ops::Range,
};
use core::ops::Range;

use crate::{
buffer::{Buffer, InsertIntoBuffer},
loom::{cell::Cell, LoomUnsafeCell},
utils::ArrayWithHeaderAndTrailer,
write::{BytesSlice, WriteBytesSlice},
};
Expand Down Expand Up @@ -61,8 +59,8 @@ where
let slice =
&buffer.0[HEADER_SIZE + index..HEADER_SIZE + index + WriteBytesSlice::size(&self)];
// SAFETY: [Cell<u8>] has the same layout as UnsafeCell<[u8]>
self.write(unsafe {
&mut *UnsafeCell::raw_get(slice as *const _ as *const UnsafeCell<[u8]>)
});
unsafe {
(*(slice as *const _ as *const LoomUnsafeCell<[u8]>)).with_mut(|s| self.write(&mut *s));
};
}
}
17 changes: 7 additions & 10 deletions src/write/vec.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use alloc::boxed::Box;
use core::{
cell::{Cell, UnsafeCell},
ops::Range,
};
use core::ops::Range;

use crate::{
buffer::{Buffer, InsertIntoBuffer, Resize},
loom::{cell::Cell, LoomUnsafeCell},
write::{BytesSlice, WriteBytesSlice},
};

Expand Down Expand Up @@ -55,9 +53,9 @@ where
let slice =
&buffer.0[HEADER_SIZE + index..HEADER_SIZE + index + WriteBytesSlice::size(&self)];
// SAFETY: [Cell<u8>] has the same layout as UnsafeCell<[u8]>
self.write(unsafe {
&mut *UnsafeCell::raw_get(slice as *const _ as *const UnsafeCell<[u8]>)
});
unsafe {
(*(slice as *const _ as *const LoomUnsafeCell<[u8]>)).with_mut(|s| self.write(&mut *s));
};
}
}

Expand All @@ -66,9 +64,8 @@ impl<const HEADER_SIZE: usize, const TRAILER_SIZE: usize> Resize
{
fn resize(&mut self, capacity: usize) {
let full_capacity = HEADER_SIZE + capacity + TRAILER_SIZE;
let buffer = alloc::vec![0u8; full_capacity].into_boxed_slice();
// SAFETY: [Cell<u8>] has the same layout as [u8]
self.0 = unsafe {
Box::from_raw(Box::into_raw(vec![0u8; full_capacity].into_boxed_slice()) as *mut _)
};
self.0 = unsafe { Box::from_raw(Box::into_raw(buffer) as *mut _) };
}
}
7 changes: 5 additions & 2 deletions src/write_vectored/array.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::{cell::Cell, io::IoSlice, mem, mem::MaybeUninit, ops::Range};
use std::{io::IoSlice, mem, mem::MaybeUninit, ops::Range};

use crate::{
buffer::{Buffer, CellBuffer, Drain},
loom::sync::atomic::{AtomicUsize, Ordering},
loom::{
cell::Cell,
sync::atomic::{AtomicUsize, Ordering},
},
utils::{init_array, ArrayWithHeaderAndTrailer},
write_vectored::{VectoredSlice, EMPTY_SLICE},
};
Expand Down
7 changes: 5 additions & 2 deletions src/write_vectored/vec.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::{cell::Cell, io::IoSlice, mem, mem::MaybeUninit, ops::Range};
use std::{io::IoSlice, mem, mem::MaybeUninit, ops::Range};

use crate::{
buffer::{Buffer, CellBuffer, Drain, Resize},
loom::sync::atomic::{AtomicUsize, Ordering},
loom::{
cell::Cell,
sync::atomic::{AtomicUsize, Ordering},
},
write_vectored::{VectoredSlice, EMPTY_SLICE},
};

Expand Down

0 comments on commit a081710

Please sign in to comment.