Skip to content
This repository was archived by the owner on Jan 4, 2026. It is now read-only.

Commit 8a1cd17

Browse files
authored
Make handle methods const (#7)
* Make some methods const * Add test, fmt
1 parent 29b4ab4 commit 8a1cd17

7 files changed

Lines changed: 74 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bbq2"
3-
version = "0.4.1"
3+
version = "0.4.2"
44
description = "A SPSC, lockless, no_std, thread safe, queue, based on BipBuffers"
55
repository = "https://github.com/jamesmunns/bbq2"
66
authors = ["James Munns <james@onevariable.com>"]

src/queue.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
use core::marker::PhantomData;
22

3-
use crate::{prod_cons::{framed::{FramedConsumer, FramedProducer}, stream::{StreamConsumer, StreamProducer}}, traits::{
4-
bbqhdl::BbqHandle, coordination::Coord, notifier::Notifier, storage::{ConstStorage, Storage}
5-
}};
3+
use crate::{
4+
prod_cons::{
5+
framed::{FramedConsumer, FramedProducer},
6+
stream::{StreamConsumer, StreamProducer},
7+
},
8+
traits::{
9+
coordination::Coord,
10+
notifier::Notifier,
11+
storage::{ConstStorage, Storage},
12+
},
13+
};
14+
15+
#[cfg(feature = "std")]
16+
use crate::traits::bbqhdl::BbqHandle;
617

718
/// A standard bbqueue
819
pub struct BBQueue<S, C, N> {
@@ -43,32 +54,27 @@ impl<S: ConstStorage, C: Coord, N: Notifier> BBQueue<S, C, N> {
4354
}
4455
}
4556

46-
4757
impl<S: Storage, C: Coord, N: Notifier> BBQueue<S, C, N> {
48-
pub fn framed_producer(&self) -> FramedProducer<&'_ Self> {
58+
pub const fn framed_producer(&self) -> FramedProducer<&'_ Self> {
4959
FramedProducer {
50-
bbq: self.bbq_ref(),
60+
bbq: self,
5161
pd: PhantomData,
5262
}
5363
}
5464

55-
pub fn framed_consumer(&self) -> FramedConsumer<&'_ Self> {
65+
pub const fn framed_consumer(&self) -> FramedConsumer<&'_ Self> {
5666
FramedConsumer {
57-
bbq: self.bbq_ref(),
67+
bbq: self,
5868
pd: PhantomData,
5969
}
6070
}
6171

62-
pub fn stream_producer(&self) -> StreamProducer<&'_ Self> {
63-
StreamProducer {
64-
bbq: self.bbq_ref(),
65-
}
72+
pub const fn stream_producer(&self) -> StreamProducer<&'_ Self> {
73+
StreamProducer { bbq: self }
6674
}
6775

68-
pub fn stream_consumer(&self) -> StreamConsumer<&'_ Self> {
69-
StreamConsumer {
70-
bbq: self.bbq_ref(),
71-
}
76+
pub const fn stream_consumer(&self) -> StreamConsumer<&'_ Self> {
77+
StreamConsumer { bbq: self }
7278
}
7379
}
7480

@@ -100,3 +106,31 @@ impl<S: Storage, C: Coord, N: Notifier> crate::queue::ArcBBQueue<S, C, N> {
100106
}
101107
}
102108
}
109+
110+
#[cfg(test)]
111+
mod test {
112+
use crate::traits::{
113+
coordination::cas::AtomicCoord, notifier::blocking::Blocking, storage::Inline,
114+
};
115+
116+
use super::*;
117+
118+
type Queue = BBQueue<Inline<4096>, AtomicCoord, Blocking>;
119+
static QUEUE: Queue = BBQueue::new();
120+
static PRODUCER: FramedProducer<&'static Queue, u16> = QUEUE.framed_producer();
121+
static CONSUMER: FramedConsumer<&'static Queue, u16> = QUEUE.framed_consumer();
122+
123+
#[test]
124+
fn handles() {
125+
let mut wgr = PRODUCER.grant(16).unwrap();
126+
wgr.iter_mut().for_each(|w| *w = 123);
127+
wgr.commit(16);
128+
129+
let rgr = CONSUMER.read().unwrap();
130+
assert_eq!(rgr.len(), 16);
131+
for b in rgr.iter() {
132+
assert_eq!(*b, 123);
133+
}
134+
rgr.release();
135+
}
136+
}

src/traits/bbqhdl.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
1818
use core::{marker::PhantomData, ops::Deref};
1919

20-
use crate::{prod_cons::{framed::{FramedConsumer, FramedProducer, LenHeader}, stream::{StreamConsumer, StreamProducer}}, queue::BBQueue};
20+
use crate::{
21+
prod_cons::{
22+
framed::{FramedConsumer, FramedProducer, LenHeader},
23+
stream::{StreamConsumer, StreamProducer},
24+
},
25+
queue::BBQueue,
26+
};
2127

2228
use super::{coordination::Coord, notifier::Notifier, storage::Storage};
2329

src/traits/coordination/cas.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ unsafe impl Coord for AtomicCoord {
6565
self.last.store(0, Ordering::Release);
6666
}
6767

68-
fn grant_max_remaining(&self, capacity: usize, mut sz: usize) -> Result<(usize, usize), WriteGrantError> {
68+
fn grant_max_remaining(
69+
&self,
70+
capacity: usize,
71+
mut sz: usize,
72+
) -> Result<(usize, usize), WriteGrantError> {
6973
if self.write_in_progress.swap(true, Ordering::AcqRel) {
7074
return Err(WriteGrantError::GrantInProgress);
7175
}

src/traits/coordination/cs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ unsafe impl Coord for CsCoord {
7171
self.last.store(0, Ordering::Release);
7272
}
7373

74-
fn grant_max_remaining(&self, capacity: usize, mut sz: usize) -> Result<(usize, usize), WriteGrantError> {
74+
fn grant_max_remaining(
75+
&self,
76+
capacity: usize,
77+
mut sz: usize,
78+
) -> Result<(usize, usize), WriteGrantError> {
7579
critical_section::with(|_cs| {
7680
if self.write_in_progress.load(Ordering::Relaxed) {
7781
return Err(WriteGrantError::GrantInProgress);

src/traits/coordination/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ pub unsafe trait Coord {
5454

5555
// Write Grants
5656

57-
fn grant_max_remaining(&self, capacity: usize, sz: usize) -> Result<(usize, usize), WriteGrantError>;
57+
fn grant_max_remaining(
58+
&self,
59+
capacity: usize,
60+
sz: usize,
61+
) -> Result<(usize, usize), WriteGrantError>;
5862
fn grant_exact(&self, capacity: usize, sz: usize) -> Result<usize, WriteGrantError>;
5963
fn commit_inner(&self, capacity: usize, grant_len: usize, used: usize);
6064

0 commit comments

Comments
 (0)