Merge pull request #3829 from lsartory/fix_3828

Fix issue #3828
This commit is contained in:
Dario Nieuwenhuis 2025-02-02 10:48:04 +00:00 committed by GitHub
commit ae5ad91bbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -35,7 +35,7 @@ use crate::waitqueue::WakerRegistration;
/// The channel requires a buffer of recyclable elements. Writing to the channel is done through /// The channel requires a buffer of recyclable elements. Writing to the channel is done through
/// an `&mut T`. /// an `&mut T`.
pub struct Channel<'a, M: RawMutex, T> { pub struct Channel<'a, M: RawMutex, T> {
buf: *mut T, buf: BufferPtr<T>,
phantom: PhantomData<&'a mut T>, phantom: PhantomData<&'a mut T>,
state: Mutex<M, RefCell<State>>, state: Mutex<M, RefCell<State>>,
} }
@ -50,7 +50,7 @@ impl<'a, M: RawMutex, T> Channel<'a, M, T> {
assert!(len != 0); assert!(len != 0);
Self { Self {
buf: buf.as_mut_ptr(), buf: BufferPtr(buf.as_mut_ptr()),
phantom: PhantomData, phantom: PhantomData,
state: Mutex::new(RefCell::new(State { state: Mutex::new(RefCell::new(State {
capacity: len, capacity: len,
@ -94,6 +94,18 @@ impl<'a, M: RawMutex, T> Channel<'a, M, T> {
} }
} }
#[repr(transparent)]
struct BufferPtr<T>(*mut T);
impl<T> BufferPtr<T> {
unsafe fn add(&self, count: usize) -> *mut T {
self.0.add(count)
}
}
unsafe impl<T> Send for BufferPtr<T> {}
unsafe impl<T> Sync for BufferPtr<T> {}
/// Send-only access to a [`Channel`]. /// Send-only access to a [`Channel`].
pub struct Sender<'a, M: RawMutex, T> { pub struct Sender<'a, M: RawMutex, T> {
channel: &'a Channel<'a, M, T>, channel: &'a Channel<'a, M, T>,