Skip to content

Commit 9e7da0c

Browse files
committed
Introduce peek() method, for direct, non-mutable output buffer access
1 parent 67edd88 commit 9e7da0c

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/lib.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@
6060
//! // Manually fetch the buffer update from the consumer interface
6161
//! buf_output.update();
6262
//!
63-
//! // Acquire a mutable reference to the output buffer
64-
//! let output = buf_output.output_buffer();
63+
//! // Acquire reference to the output buffer
64+
//! let output = buf_output.peek_output_buffer();
65+
//! assert_eq!(*output, "Hello, ");
66+
//!
67+
//! // Or acquire it mutably if necessary
68+
//! let output_mut = buf_output.output_buffer();
6569
//!
6670
//! // Post-process the output value before use
67-
//! output.push_str("world!");
71+
//! output_mut.push_str("world!");
6872
//! ```
6973
7074
#![cfg_attr(not(test), no_std)]
@@ -338,6 +342,19 @@ impl<T: Send> Output<T> {
338342
back_info & BACK_DIRTY_BIT != 0
339343
}
340344

345+
/// Access the output buffer directly, in non-mutable way
346+
///
347+
/// This is simply a non-mutable version of `output_buffer()`.
348+
/// For details, see the `output_buffer()` method.
349+
///
350+
/// This method does not update the output buffer automatically. You need to call
351+
/// `update()` in order to fetch buffer updates from the producer.
352+
pub fn peek_output_buffer(&self) -> &T {
353+
// Access the output buffer directly
354+
let output_ptr = self.shared.buffers[self.output_idx as usize].get();
355+
unsafe { &*output_ptr }
356+
}
357+
341358
/// Access the output buffer directly
342359
///
343360
/// This advanced interface allows you to modify the contents of the output
@@ -881,7 +898,7 @@ mod tests {
881898
move || {
882899
let mut last_value = 0usize;
883900
while last_value < TEST_WRITE_COUNT {
884-
match buf_output.output_buffer().get() {
901+
match buf_output.peek_output_buffer().get() {
885902
Racey::Consistent(new_value) => {
886903
assert!((new_value >= last_value) && (new_value <= TEST_WRITE_COUNT));
887904
last_value = new_value;

0 commit comments

Comments
 (0)