Skip to content

Change some pub to pub(crate) in std #107901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/std/src/collections/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Unordered containers, implemented as hash-tables

pub mod map;
pub mod set;
pub(crate) mod map;
pub(crate) mod set;
2 changes: 1 addition & 1 deletion library/std/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod private {
// implementations, since that can enable unsound downcasting.
#[unstable(feature = "error_type_id", issue = "60784")]
#[derive(Debug)]
pub struct Internal;
pub(crate) struct Internal;
}

/// An error reporter that prints an error and its sources.
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ macro_rules! error_contains {
// have permission, and return otherwise. This way, we still don't run these
// tests most of the time, but at least we do if the user has the right
// permissions.
pub fn got_symlink_permission(tmpdir: &TempDir) -> bool {
pub(crate) fn got_symlink_permission(tmpdir: &TempDir) -> bool {
if cfg!(unix) {
return true;
}
Expand Down
24 changes: 12 additions & 12 deletions library/std/src/io/buffered/bufreader/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::cmp;
use crate::io::{self, BorrowedBuf, Read};
use crate::mem::MaybeUninit;

pub struct Buffer {
pub(crate) struct Buffer {
// The buffer.
buf: Box<[MaybeUninit<u8>]>,
// The current seek offset into `buf`, must always be <= `filled`.
Expand All @@ -30,54 +30,54 @@ pub struct Buffer {

impl Buffer {
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
pub(crate) fn with_capacity(capacity: usize) -> Self {
let buf = Box::new_uninit_slice(capacity);
Self { buf, pos: 0, filled: 0, initialized: 0 }
}

#[inline]
pub fn buffer(&self) -> &[u8] {
pub(crate) fn buffer(&self) -> &[u8] {
// SAFETY: self.pos and self.cap are valid, and self.cap => self.pos, and
// that region is initialized because those are all invariants of this type.
unsafe { MaybeUninit::slice_assume_init_ref(self.buf.get_unchecked(self.pos..self.filled)) }
}

#[inline]
pub fn capacity(&self) -> usize {
pub(crate) fn capacity(&self) -> usize {
self.buf.len()
}

#[inline]
pub fn filled(&self) -> usize {
pub(crate) fn filled(&self) -> usize {
self.filled
}

#[inline]
pub fn pos(&self) -> usize {
pub(crate) fn pos(&self) -> usize {
self.pos
}

// This is only used by a test which asserts that the initialization-tracking is correct.
#[cfg(test)]
pub fn initialized(&self) -> usize {
pub(crate) fn initialized(&self) -> usize {
self.initialized
}

#[inline]
pub fn discard_buffer(&mut self) {
pub(crate) fn discard_buffer(&mut self) {
self.pos = 0;
self.filled = 0;
}

#[inline]
pub fn consume(&mut self, amt: usize) {
pub(crate) fn consume(&mut self, amt: usize) {
self.pos = cmp::min(self.pos + amt, self.filled);
}

/// If there are `amt` bytes available in the buffer, pass a slice containing those bytes to
/// `visitor` and return true. If there are not enough bytes available, return false.
#[inline]
pub fn consume_with<V>(&mut self, amt: usize, mut visitor: V) -> bool
pub(crate) fn consume_with<V>(&mut self, amt: usize, mut visitor: V) -> bool
where
V: FnMut(&[u8]),
{
Expand All @@ -92,12 +92,12 @@ impl Buffer {
}

#[inline]
pub fn unconsume(&mut self, amt: usize) {
pub(crate) fn unconsume(&mut self, amt: usize) {
self.pos = self.pos.saturating_sub(amt);
}

#[inline]
pub fn fill_buf(&mut self, mut reader: impl Read) -> io::Result<&[u8]> {
pub(crate) fn fill_buf(&mut self, mut reader: impl Read) -> io::Result<&[u8]> {
// If we've reached the end of our internal buffer then we need to fetch
// some more data from the reader.
// Branch using `>=` instead of the more correct `==`
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/io/buffered/linewritershim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use crate::sys_common::memchr;
/// `BufWriters` to be temporarily given line-buffering logic; this is what
/// enables Stdout to be alternately in line-buffered or block-buffered mode.
#[derive(Debug)]
pub struct LineWriterShim<'a, W: Write> {
pub(crate) struct LineWriterShim<'a, W: Write> {
buffer: &'a mut BufWriter<W>,
}

impl<'a, W: Write> LineWriterShim<'a, W> {
pub fn new(buffer: &'a mut BufWriter<W>) -> Self {
pub(crate) fn new(buffer: &'a mut BufWriter<W>) -> Self {
Self { buffer }
}

Expand Down
16 changes: 8 additions & 8 deletions library/std/src/io/buffered/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::thread;

/// A dummy reader intended at testing short-reads propagation.
pub struct ShortReader {
pub(crate) struct ShortReader {
lengths: Vec<usize>,
}

Expand Down Expand Up @@ -541,25 +541,25 @@ fn bench_buffered_writer(b: &mut test::Bencher) {
#[derive(Default, Clone)]
struct ProgrammableSink {
// Writes append to this slice
pub buffer: Vec<u8>,
pub(crate) buffer: Vec<u8>,

// If true, writes will always be an error
pub always_write_error: bool,
pub(crate) always_write_error: bool,

// If true, flushes will always be an error
pub always_flush_error: bool,
pub(crate) always_flush_error: bool,

// If set, only up to this number of bytes will be written in a single
// call to `write`
pub accept_prefix: Option<usize>,
pub(crate) accept_prefix: Option<usize>,

// If set, counts down with each write, and writes return an error
// when it hits 0
pub max_writes: Option<usize>,
pub(crate) max_writes: Option<usize>,

// If set, attempting to write when max_writes == Some(0) will be an
// error; otherwise, it will return Ok(0).
pub error_after_max_writes: bool,
pub(crate) error_after_max_writes: bool,
}

impl Write for ProgrammableSink {
Expand Down Expand Up @@ -1006,7 +1006,7 @@ enum RecordedEvent {

#[derive(Debug, Clone, Default)]
struct WriteRecorder {
pub events: Vec<RecordedEvent>,
pub(crate) events: Vec<RecordedEvent>,
}

impl Write for WriteRecorder {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/io/cursor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ fn test_partial_eq() {

#[test]
fn test_eq() {
struct AssertEq<T: Eq>(pub T);
struct AssertEq<T: Eq>(pub(crate) T);

let _: AssertEq<Cursor<Vec<u8>>> = AssertEq(Cursor::new(Vec::new()));
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ pub fn stdout() -> Stdout {
// Flush the data and disable buffering during shutdown
// by replacing the line writer by one with zero
// buffering capacity.
pub fn cleanup() {
pub(crate) fn cleanup() {
let mut initialized = false;
let stdout = STDOUT.get_or_init(|| {
initialized = true;
Expand Down
8 changes: 7 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
#![allow(unused_lifetimes)]
#![deny(rustc::existing_doc_keyword)]
#![deny(fuzzy_provenance_casts)]
#![cfg_attr(not(test), warn(unreachable_pub))]
// Ensure that std can be linked against panic_abort despite compiled with `-C panic=unwind`
#![deny(ffi_unwind_calls)]
// std may use features in a platform-specific way
Expand Down Expand Up @@ -518,6 +519,8 @@ pub mod fs;
pub mod io;
pub mod net;
pub mod num;
// os-specific code may be incorrectly detected as unreachable
#[allow(unreachable_pub)]
pub mod os;
pub mod panic;
pub mod path;
Expand Down Expand Up @@ -582,7 +585,10 @@ pub mod arch {
pub use std_detect::is_x86_feature_detected;

// Platform-abstraction modules
// platform-specific code may be incorrectly detected as unreachable
#[allow(unreachable_pub)]
mod sys;
#[allow(unreachable_pub)]
mod sys_common;

pub mod alloc;
Expand All @@ -592,7 +598,7 @@ mod panicking;
mod personality;

#[path = "../../backtrace/src/lib.rs"]
#[allow(dead_code, unused_attributes, fuzzy_provenance_casts)]
#[allow(dead_code, unused_attributes, fuzzy_provenance_casts, unreachable_pub)]
mod backtrace_rs;

// Re-export macros defined in core.
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/net/display_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use crate::mem::MaybeUninit;
use crate::str;

/// Used for slow path in `Display` implementations when alignment is required.
pub struct DisplayBuffer<const SIZE: usize> {
pub(crate) struct DisplayBuffer<const SIZE: usize> {
buf: [MaybeUninit<u8>; SIZE],
len: usize,
}

impl<const SIZE: usize> DisplayBuffer<SIZE> {
#[inline]
pub const fn new() -> Self {
pub(crate) const fn new() -> Self {
Self { buf: MaybeUninit::uninit_array(), len: 0 }
}

#[inline]
pub fn as_str(&self) -> &str {
pub(crate) fn as_str(&self) -> &str {
// SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
// which writes a valid UTF-8 string to `buf` and correctly sets `len`.
unsafe {
Expand Down
10 changes: 5 additions & 5 deletions library/std/src/net/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ use crate::sync::atomic::{AtomicUsize, Ordering};

static PORT: AtomicUsize = AtomicUsize::new(0);

pub fn next_test_ip4() -> SocketAddr {
pub(crate) fn next_test_ip4() -> SocketAddr {
let port = PORT.fetch_add(1, Ordering::SeqCst) as u16 + base_port();
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port))
}

pub fn next_test_ip6() -> SocketAddr {
pub(crate) fn next_test_ip6() -> SocketAddr {
let port = PORT.fetch_add(1, Ordering::SeqCst) as u16 + base_port();
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), port, 0, 0))
}

pub fn sa4(a: Ipv4Addr, p: u16) -> SocketAddr {
pub(crate) fn sa4(a: Ipv4Addr, p: u16) -> SocketAddr {
SocketAddr::V4(SocketAddrV4::new(a, p))
}

pub fn sa6(a: Ipv6Addr, p: u16) -> SocketAddr {
pub(crate) fn sa6(a: Ipv6Addr, p: u16) -> SocketAddr {
SocketAddr::V6(SocketAddrV6::new(a, p, 0, 0))
}

pub fn tsa<A: ToSocketAddrs>(a: A) -> Result<Vec<SocketAddr>, String> {
pub(crate) fn tsa<A: ToSocketAddrs>(a: A) -> Result<Vec<SocketAddr>, String> {
match a.to_socket_addrs() {
Ok(a) => Ok(a.collect()),
Err(e) => Err(e.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/os/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod platform {
#[cfg(target_os = "l4re")]
pub use crate::os::l4re::*;
#[cfg(target_os = "linux")]
pub use crate::os::linux::*;
pub(crate) use crate::os::linux::*;
#[cfg(target_os = "macos")]
pub use crate::os::macos::*;
#[cfg(target_os = "netbsd")]
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub mod panic_count {
pub use realstd::rt::panic_count;

/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
pub(crate) unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
union Data<F, R> {
f: ManuallyDrop<F>,
r: ManuallyDrop<R>,
Expand Down Expand Up @@ -517,14 +517,14 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>

/// Determines whether the current thread is unwinding because of panic.
#[inline]
pub fn panicking() -> bool {
pub(crate) fn panicking() -> bool {
!panic_count::count_is_zero()
}

/// Entry point of panics from the core crate (`panic_impl` lang item).
#[cfg(not(test))]
#[panic_handler]
pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
pub(crate) fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
struct PanicPayload<'a> {
inner: &'a fmt::Arguments<'a>,
string: Option<String>,
Expand Down Expand Up @@ -716,7 +716,7 @@ fn rust_panic_with_hook(

/// This is the entry point for `resume_unwind`.
/// It just forwards the payload to the panic runtime.
pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
pub(crate) fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
panic_count::increase();

struct RewrapBox(Box<dyn Any + Send>);
Expand Down
Loading