Skip to content

Commit ed009e7

Browse files
committed
Reduce API surface of Buffer
None of the currently public methods are accessible outside `std`, and are unused within. Therefore, they can be restricted to internal use.
1 parent 473b77c commit ed009e7

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

  • library/alloc/src/io/buffered/bufreader

‎library/alloc/src/io/buffered/bufreader/buffer.rs‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use core::mem::MaybeUninit;
1515
use crate::boxed::Box;
1616
use crate::io::{self, BorrowedBuf, ErrorKind, Read};
1717

18-
#[expect(missing_debug_implementations)]
18+
#[doc(hidden)]
19+
#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
20+
#[derive(Debug)]
1921
pub struct Buffer {
2022
// The buffer.
2123
buf: Box<[MaybeUninit<u8>]>,
@@ -34,15 +36,13 @@ pub struct Buffer {
3436
impl Buffer {
3537
#[cfg(not(no_global_oom_handling))]
3638
#[inline]
37-
pub fn with_capacity(capacity: usize) -> Self {
39+
pub(super) fn with_capacity(capacity: usize) -> Self {
3840
let buf = Box::new_uninit_slice(capacity);
3941
Self { buf, pos: 0, filled: 0, initialized: false }
4042
}
4143

42-
#[doc(hidden)]
43-
#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
4444
#[inline]
45-
pub fn try_with_capacity(capacity: usize) -> io::Result<Self> {
45+
pub(super) fn try_with_capacity(capacity: usize) -> io::Result<Self> {
4646
match Box::try_new_uninit_slice(capacity) {
4747
Ok(buf) => Ok(Self { buf, pos: 0, filled: 0, initialized: false }),
4848
Err(_) => {
@@ -52,49 +52,47 @@ impl Buffer {
5252
}
5353

5454
#[inline]
55-
pub fn buffer(&self) -> &[u8] {
55+
pub(super) fn buffer(&self) -> &[u8] {
5656
// SAFETY: self.pos and self.filled are valid, and self.filled >= self.pos, and
5757
// that region is initialized because those are all invariants of this type.
5858
unsafe { self.buf.get_unchecked(self.pos..self.filled).assume_init_ref() }
5959
}
6060

6161
#[inline]
62-
pub fn capacity(&self) -> usize {
62+
pub(super) fn capacity(&self) -> usize {
6363
self.buf.len()
6464
}
6565

6666
#[inline]
67-
pub fn filled(&self) -> usize {
67+
pub(super) fn filled(&self) -> usize {
6868
self.filled
6969
}
7070

7171
#[inline]
72-
pub fn pos(&self) -> usize {
72+
pub(super) fn pos(&self) -> usize {
7373
self.pos
7474
}
7575

7676
// This is only used by a test which asserts that the initialization-tracking is correct.
77-
#[doc(hidden)]
78-
#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
79-
pub fn initialized(&self) -> bool {
77+
pub(super) fn initialized(&self) -> bool {
8078
self.initialized
8179
}
8280

8381
#[inline]
84-
pub fn discard_buffer(&mut self) {
82+
pub(super) fn discard_buffer(&mut self) {
8583
self.pos = 0;
8684
self.filled = 0;
8785
}
8886

8987
#[inline]
90-
pub fn consume(&mut self, amt: usize) {
88+
pub(super) fn consume(&mut self, amt: usize) {
9189
self.pos = cmp::min(self.pos + amt, self.filled);
9290
}
9391

9492
/// If there are `amt` bytes available in the buffer, pass a slice containing those bytes to
9593
/// `visitor` and return true. If there are not enough bytes available, return false.
9694
#[inline]
97-
pub fn consume_with<V>(&mut self, amt: usize, mut visitor: V) -> bool
95+
pub(super) fn consume_with<V>(&mut self, amt: usize, mut visitor: V) -> bool
9896
where
9997
V: FnMut(&[u8]),
10098
{
@@ -109,12 +107,12 @@ impl Buffer {
109107
}
110108

111109
#[inline]
112-
pub fn unconsume(&mut self, amt: usize) {
110+
pub(super) fn unconsume(&mut self, amt: usize) {
113111
self.pos = self.pos.saturating_sub(amt);
114112
}
115113

116114
/// Read more bytes into the buffer without discarding any of its contents
117-
pub fn read_more(&mut self, mut reader: impl Read) -> io::Result<usize> {
115+
pub(super) fn read_more(&mut self, mut reader: impl Read) -> io::Result<usize> {
118116
let mut buf = BorrowedBuf::from(&mut self.buf[self.filled..]);
119117

120118
if self.initialized {
@@ -131,14 +129,14 @@ impl Buffer {
131129
}
132130

133131
/// Remove bytes that have already been read from the buffer.
134-
pub fn backshift(&mut self) {
132+
pub(super) fn backshift(&mut self) {
135133
self.buf.copy_within(self.pos..self.filled, 0);
136134
self.filled -= self.pos;
137135
self.pos = 0;
138136
}
139137

140138
#[inline]
141-
pub fn fill_buf(&mut self, mut reader: impl Read) -> io::Result<&[u8]> {
139+
pub(super) fn fill_buf(&mut self, mut reader: impl Read) -> io::Result<&[u8]> {
142140
// If we've reached the end of our internal buffer then we need to fetch
143141
// some more data from the reader.
144142
// Branch using `>=` instead of the more correct `==`

0 commit comments

Comments
 (0)