Skip to content

Commit b939443

Browse files
authored
Merge pull request #47 from HadrienG2/read_only_accessors
Start v9 release work by turning unsuffixed accessors read-only
2 parents 53ca478 + 4c2dc73 commit b939443

File tree

6 files changed

+38
-61
lines changed

6 files changed

+38
-61
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11-
_No unreleased changes in the pipeline at the moment_
11+
### Changed
12+
13+
- Turn `Input::input_buffer()` and `Output::output_buffer()` into read-only
14+
accessors and deprecate `Output::peek_output_buffer()`, moving forward with
15+
the plan set in issue #30 to eventually migrate towards an API naming
16+
convention that matches `std` and other Rust libraries.
1217

1318

1419

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
@@ -11,7 +11,7 @@ name = "triple_buffer"
1111
# - Roll an annotated git tag
1212
# - Add a github release
1313
#
14-
version = "8.1.1"
14+
version = "9.0.0"
1515
authors = ["Hadrien G. <[email protected]>"]
1616
description = "An implementation of triple buffering, useful for sharing frequently updated data between threads"
1717
documentation = "https://docs.rs/triple_buffer/"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ buf_input.publish();
7474
// Manually fetch the buffer update from the consumer interface
7575
buf_output.update();
7676

77-
// Acquire read-only reference to the output buffer
78-
let output = buf_output.peek_output_buffer();
77+
// Acquire a read-only reference to the output buffer
78+
let output = buf_output.output_buffer();
7979
assert_eq!(*output, "Hello, ");
8080

81-
// Or acquire mutable reference if necessary
81+
// Or acquire a mutable reference if necessary
8282
let output_mut = buf_output.output_buffer_mut();
8383

8484
// Post-process the output value before use

benches/benchmarks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn benchmark(c: &mut Criterion) {
66

77
{
88
let mut uncontended = c.benchmark_group("uncontended");
9-
uncontended.bench_function("read output", |b| b.iter(|| *output.peek_output_buffer()));
9+
uncontended.bench_function("read output", |b| b.iter(|| *output.output_buffer()));
1010
uncontended.bench_function("clean update", |b| {
1111
b.iter(|| {
1212
output.update();
@@ -79,7 +79,7 @@ pub fn benchmark(c: &mut Criterion) {
7979
|| input.write(black_box(0)),
8080
|| {
8181
write_contended
82-
.bench_function("read output", |b| b.iter(|| *output.peek_output_buffer()));
82+
.bench_function("read output", |b| b.iter(|| *output.output_buffer()));
8383
write_contended.bench_function("update", |b| {
8484
b.iter(|| {
8585
output.update();

src/lib.rs

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
//! // Manually fetch the buffer update from the consumer interface
6363
//! buf_output.update();
6464
//!
65-
//! // Acquire read-only reference to the output buffer
66-
//! let output = buf_output.peek_output_buffer();
65+
//! // Acquire a read-only reference to the output buffer
66+
//! let output = buf_output.output_buffer();
6767
//! assert_eq!(*output, "Hello, ");
6868
//!
69-
//! // Or acquire mutable reference if necessary
69+
//! // Or acquire a mutable reference if necessary
7070
//! let output_mut = buf_output.output_buffer_mut();
7171
//!
7272
//! // Post-process the output value before use
@@ -266,33 +266,17 @@ impl<T: Send> Input<T> {
266266

267267
/// Query the current value of the input buffer
268268
///
269-
/// This is simply a read-only version of
269+
/// This is a read-only version of
270270
/// [`input_buffer_mut()`](Input::input_buffer_mut). Please read the
271271
/// documentation of that method for more information on the precautions
272272
/// that need to be taken when accessing the input buffer in place.
273-
fn peek_input_buffer(&self) -> &T {
274-
// Access the input buffer directly
273+
pub fn input_buffer(&self) -> &T {
274+
// This is safe because the synchronization protocol ensures that we
275+
// have exclusive access to this buffer.
275276
let input_ptr = self.shared.buffers[self.input_idx as usize].get();
276277
unsafe { &*input_ptr }
277278
}
278279

279-
/// Access the input buffer directly
280-
///
281-
/// This is, for now, a deprecated alias to
282-
/// [`input_buffer_mut()`](Input::input_buffer_mut). Please use this method
283-
/// instead.
284-
///
285-
/// In a future major release of this crate, `input_buffer()` will
286-
/// undergo a breaking change to instead provide read-only access.
287-
///
288-
/// The aim of this process is to eventually migrate towards the standard
289-
/// `accessor()`/`accessor_mut()` method naming convention that most Rust
290-
/// libraries follow.
291-
#[deprecated = "Please use input_buffer_mut() instead"]
292-
pub fn input_buffer(&mut self) -> &mut T {
293-
self.input_buffer_mut()
294-
}
295-
296280
/// Access the input buffer directly
297281
///
298282
/// This advanced interface allows you to update the input buffer in place,
@@ -398,7 +382,7 @@ impl<T: Send> Deref for InputPublishGuard<'_, T> {
398382
type Target = T;
399383

400384
fn deref(&self) -> &T {
401-
self.reference.peek_input_buffer()
385+
self.reference.input_buffer()
402386
}
403387
}
404388

@@ -467,44 +451,32 @@ impl<T: Send> Output<T> {
467451

468452
/// Query the current value of the output buffer
469453
///
470-
/// This is simply a read-only version of
454+
/// This is a deprecated compatibility alias to
455+
/// [`output_buffer()`](Self::output_buffer). Please use that method
456+
/// instead, as `peek_output_buffer()` is scheduled for removal in the next
457+
/// major release of `triple-buffer`.
458+
#[deprecated = "Please use output_buffer() instead"]
459+
pub fn peek_output_buffer(&self) -> &T {
460+
self.output_buffer()
461+
}
462+
463+
/// Query the current value of the output buffer
464+
///
465+
/// This is a read-only version of
471466
/// [`output_buffer_mut()`](Output::output_buffer_mut). Please read the
472467
/// documentation of that method for more information on the precautions
473468
/// that need to be taken when accessing the output buffer in place.
474469
///
475470
/// In particular, remember that this method does not update the output
476471
/// buffer automatically. You need to call [`update()`](Output::update) in
477472
/// order to fetch buffer updates from the producer.
478-
pub fn peek_output_buffer(&self) -> &T {
479-
// Access the output buffer directly
473+
pub fn output_buffer(&self) -> &T {
474+
// This is safe because the synchronization protocol ensures that we
475+
// have exclusive access to this buffer.
480476
let output_ptr = self.shared.buffers[self.output_idx as usize].get();
481477
unsafe { &*output_ptr }
482478
}
483479

484-
/// Access the input buffer directly
485-
///
486-
/// This is, for now, a deprecated alias to [`output_buffer_mut()`]. Please
487-
/// use this method instead.
488-
///
489-
/// In a future major release of this crate, `output_buffer()` will undergo
490-
/// a breaking change to instead provide read-only access, like
491-
/// [`peek_output_buffer()`] currently does. At that point,
492-
/// [`peek_output_buffer()`] will be deprecated.
493-
///
494-
/// Finally, in a later major release [`peek_output_buffer()`] will be
495-
/// removed.
496-
///
497-
/// The aim of this process is to eventually migrate towards the standard
498-
/// `accessor()`/`accessor_mut()` method naming convention that most Rust
499-
/// libraries follow.
500-
///
501-
/// [`output_buffer_mut()`]: Output::output_buffer_mut
502-
/// [`peek_output_buffer()`]: Output::peek_output_buffer
503-
#[deprecated = "Please use output_buffer_mut() instead"]
504-
pub fn output_buffer(&mut self) -> &mut T {
505-
self.output_buffer_mut()
506-
}
507-
508480
/// Access the output buffer directly
509481
///
510482
/// This advanced interface allows you to modify the contents of the output
@@ -1169,7 +1141,7 @@ mod tests {
11691141
move || {
11701142
let mut last_value = 0usize;
11711143
while last_value < TEST_WRITE_COUNT {
1172-
match buf_output.peek_output_buffer().get() {
1144+
match buf_output.output_buffer().get() {
11731145
Racey::Consistent(new_value) => {
11741146
assert!((new_value >= last_value) && (new_value <= TEST_WRITE_COUNT));
11751147
last_value = new_value;
@@ -1240,7 +1212,7 @@ mod tests {
12401212

12411213
// Check that the output_buffer query works in the initial state
12421214
assert_eq!(
1243-
as_ptr(&buf.output.peek_output_buffer()),
1215+
as_ptr(&buf.output.output_buffer()),
12441216
buf.output.shared.buffers[buf.output.output_idx as usize].get()
12451217
);
12461218
assert_eq!(*buf, initial_buf);

0 commit comments

Comments
 (0)