|
60 | 60 | //! // Manually fetch the buffer update from the consumer interface |
61 | 61 | //! buf_output.update(); |
62 | 62 | //! |
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(); |
65 | 69 | //! |
66 | 70 | //! // Post-process the output value before use |
67 | | -//! output.push_str("world!"); |
| 71 | +//! output_mut.push_str("world!"); |
68 | 72 | //! ``` |
69 | 73 |
|
70 | 74 | #![cfg_attr(not(test), no_std)] |
@@ -338,6 +342,19 @@ impl<T: Send> Output<T> { |
338 | 342 | back_info & BACK_DIRTY_BIT != 0 |
339 | 343 | } |
340 | 344 |
|
| 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 | + |
341 | 358 | /// Access the output buffer directly |
342 | 359 | /// |
343 | 360 | /// This advanced interface allows you to modify the contents of the output |
@@ -881,7 +898,7 @@ mod tests { |
881 | 898 | move || { |
882 | 899 | let mut last_value = 0usize; |
883 | 900 | while last_value < TEST_WRITE_COUNT { |
884 | | - match buf_output.output_buffer().get() { |
| 901 | + match buf_output.peek_output_buffer().get() { |
885 | 902 | Racey::Consistent(new_value) => { |
886 | 903 | assert!((new_value >= last_value) && (new_value <= TEST_WRITE_COUNT)); |
887 | 904 | last_value = new_value; |
|
0 commit comments