caio.co/de/foca

Fail compilation on unreachable public exports

This enables the unreachable_pub rustc lint so that we don't end
up exposing unconstructable types again

Ref: https://github.com/caio/foca/issues/24
Id
04fd49c75d478792abc6fce1e93cab44a7080352
Author
Caio
Commit time
2023-07-10T12:35:25+02:00

Modified src/broadcast.rs

@@ -106,17 +106,17
where
T: Invalidates + AsRef<[u8]>,
{
- pub fn new() -> Self {
+ pub(crate) fn new() -> Self {
Self {
storage: Vec::new(),
}
}

- pub fn len(&self) -> usize {
+ pub(crate) fn len(&self) -> usize {
self.storage.len()
}

- pub fn add_or_replace(&mut self, value: T, max_tx: usize) {
+ pub(crate) fn add_or_replace(&mut self, value: T, max_tx: usize) {
let new_node = Entry {
remaining_tx: max_tx,
value,
@@ -141,7 +141,7
self.storage.insert(position, new_node);
}

- pub fn fill(&mut self, mut buffer: impl BufMut, max_items: usize) -> usize {
+ pub(crate) fn fill(&mut self, mut buffer: impl BufMut, max_items: usize) -> usize {
if self.storage.is_empty() {
return 0;
}
@@ -201,14 +201,14
num_taken
}

- pub fn is_sorted(&self) -> bool {
+ pub(crate) fn is_sorted(&self) -> bool {
// Future: `is_sorted` from https://github.com/rust-lang/rfcs/pull/2351
self.storage[..]
.windows(2)
.all(|w| w[0].remaining_tx <= w[1].remaining_tx)
}

- pub fn is_empty(&self) -> bool {
+ pub(crate) fn is_empty(&self) -> bool {
self.storage.is_empty()
}
}

Modified src/codec.rs

@@ -8,10 +8,10
use crate::{Header, Member};

#[cfg(feature = "bincode-codec")]
-pub mod bincode_impl;
+pub(crate) mod bincode_impl;

#[cfg(feature = "postcard-codec")]
-pub mod postcard_impl;
+pub(crate) mod postcard_impl;

/// A Codec is responsible to encoding and decoding the data that
/// is sent between cluster members.

Modified src/lib.rs

@@ -42,7 +42,7
//!
#![forbid(unsafe_code)]
#![no_std]
-#![deny(missing_docs)]
+#![deny(missing_docs, unreachable_pub)]
#![deny(rustdoc::broken_intra_doc_links)]

extern crate alloc;
@@ -1555,7 +1555,7
self.incarnation
}

- pub fn probe(&self) -> &Probe<T> {
+ pub(crate) fn probe(&self) -> &Probe<T> {
&self.probe
}

Modified src/member.rs

@@ -141,17 +141,17

#[cfg(test)]
impl<T> Members<T> {
- pub fn len(&self) -> usize {
+ pub(crate) fn len(&self) -> usize {
self.inner.len()
}
}

impl<T: PartialEq + Clone> Members<T> {
- pub fn num_active(&self) -> usize {
+ pub(crate) fn num_active(&self) -> usize {
self.num_active
}

- pub fn new(inner: Vec<Member<T>>) -> Self {
+ pub(crate) fn new(inner: Vec<Member<T>>) -> Self {
// XXX This doesn't prevent someone initializing with
// duplicated members... Not a problem (yet?) since
// inner is always empty outside of tests
@@ -166,7 +166,7

// Next member that's considered active
// Chosen at random (shuffle + round-robin)
- pub fn next(&mut self, mut rng: impl Rng) -> Option<&Member<T>> {
+ pub(crate) fn next(&mut self, mut rng: impl Rng) -> Option<&Member<T>> {
// Round-robin with a shuffle at the end
if self.cursor >= self.inner.len() {
self.inner.shuffle(&mut rng);
@@ -215,7 +215,7
/// a high chance that every member will be *pinged* periodically
/// and using the same logic for other "pick random member"
/// mechanisms might break the math.
- pub fn choose_active_members<F>(
+ pub(crate) fn choose_active_members<F>(
&self,
wanted: usize,
output: &mut Vec<Member<T>>,
@@ -246,7 +246,7
}
}

- pub fn remove_if_down(&mut self, id: &T) -> Option<Member<T>> {
+ pub(crate) fn remove_if_down(&mut self, id: &T) -> Option<Member<T>> {
let position = self
.inner
.iter()
@@ -255,11 +255,11
position.map(|pos| self.inner.swap_remove(pos))
}

- pub fn iter_active(&self) -> impl Iterator<Item = &Member<T>> {
+ pub(crate) fn iter_active(&self) -> impl Iterator<Item = &Member<T>> {
self.inner.iter().filter(|m| m.is_active())
}

- pub fn apply_existing_if<F: Fn(&Member<T>) -> bool>(
+ pub(crate) fn apply_existing_if<F: Fn(&Member<T>) -> bool>(
&mut self,
update: Member<T>,
condition: F,
@@ -300,7 +300,7
}
}

- pub fn apply(&mut self, update: Member<T>, mut rng: impl Rng) -> ApplySummary {
+ pub(crate) fn apply(&mut self, update: Member<T>, mut rng: impl Rng) -> ApplySummary {
self.apply_existing_if(update.clone(), |_member| true)
.unwrap_or_else(|| {
// Unknown member, we'll register it
@@ -333,9 +333,9
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub(crate) struct ApplySummary {
- pub is_active_now: bool,
- pub apply_successful: bool,
- pub changed_active_set: bool,
+ pub(crate) is_active_now: bool,
+ pub(crate) apply_successful: bool,
+ pub(crate) changed_active_set: bool,
}

#[cfg(test)]

Modified src/probe.rs

@@ -8,7 +8,7

// FIXME This whole thing is ugly AF :(

-pub struct Probe<T> {
+pub(crate) struct Probe<T> {
direct: Option<Member<T>>,
indirect: Vec<T>,
probe_number: ProbeNumber,
@@ -20,7 +20,7
}

impl<T: Clone + PartialEq> Probe<T> {
- pub fn new(indirect: Vec<T>) -> Self {
+ pub(crate) fn new(indirect: Vec<T>) -> Self {
Self {
indirect,
direct: None,
@@ -32,7 +32,7
}

#[must_use]
- pub fn start(&mut self, target: Member<T>) -> ProbeNumber {
+ pub(crate) fn start(&mut self, target: Member<T>) -> ProbeNumber {
self.clear();
self.direct = Some(target);
self.probe_number = self.probe_number.wrapping_add(1);
@@ -43,7 +43,7
self.probe_number
}

- pub fn clear(&mut self) {
+ pub(crate) fn clear(&mut self) {
self.direct = None;
self.indirect.clear();
self.direct_ack_ok = false;
@@ -52,11 +52,11
// do NOT reset probe_number
}

- pub fn mark_indirect_probe_stage_reached(&mut self) {
+ pub(crate) fn mark_indirect_probe_stage_reached(&mut self) {
self.reached_indirect_probe_stage = true;
}

- pub fn validate(&self) -> bool {
+ pub(crate) fn validate(&self) -> bool {
// A probe that hasn't been started is
// valid
self.direct.is_none()
@@ -65,7 +65,7
|| self.reached_indirect_probe_stage
}

- pub fn take_failed(&mut self) -> Option<Member<T>> {
+ pub(crate) fn take_failed(&mut self) -> Option<Member<T>> {
if !self.succeeded() {
self.direct.take()
} else {
@@ -74,22 +74,22
}

#[cfg(any(feature = "tracing", test))]
- pub fn target(&self) -> Option<&T> {
+ pub(crate) fn target(&self) -> Option<&T> {
self.direct.as_ref().map(|probed| probed.id())
}

- pub fn is_probing(&self, id: &T) -> bool {
+ pub(crate) fn is_probing(&self, id: &T) -> bool {
self.direct
.as_ref()
.map(|probed| probed.id() == id)
.unwrap_or(false)
}

- pub fn succeeded(&self) -> bool {
+ pub(crate) fn succeeded(&self) -> bool {
self.direct_ack_ok || self.indirect_ack_count > 0
}

- pub fn receive_ack(&mut self, from: &T, probeno: ProbeNumber) -> bool {
+ pub(crate) fn receive_ack(&mut self, from: &T, probeno: ProbeNumber) -> bool {
if probeno == self.probe_number
&& self
.direct
@@ -104,7 +104,7
}
}

- pub fn expect_indirect_ack(&mut self, from: T) {
+ pub(crate) fn expect_indirect_ack(&mut self, from: T) {
debug_assert!(self
.direct
.as_ref()
@@ -113,7 +113,7
self.indirect.push(from);
}

- pub fn receive_indirect_ack(&mut self, from: &T, probeno: ProbeNumber) -> bool {
+ pub(crate) fn receive_indirect_ack(&mut self, from: &T, probeno: ProbeNumber) -> bool {
if self.probe_number != probeno {
return false;
}

Modified src/testing.rs

@@ -9,7 +9,7
use crate::{Codec, Header, Identity, Member, Message, Notification, Runtime, State, Timer};

#[derive(Debug, Clone, Copy, PartialOrd, Ord)]
-pub struct ID {
+pub(crate) struct ID {
id: u8,
bump: u8,
rejoinable: bool,
@@ -25,11 +25,11
impl Eq for ID {}

impl ID {
- pub fn new(id: u8) -> Self {
+ pub(crate) fn new(id: u8) -> Self {
ID::new_with_bump(id, 0)
}

- pub fn new_with_bump(id: u8, bump: u8) -> Self {
+ pub(crate) fn new_with_bump(id: u8, bump: u8) -> Self {
Self {
id,
bump,
@@ -37,12 +37,12
}
}

- pub fn rejoinable(mut self) -> Self {
+ pub(crate) fn rejoinable(mut self) -> Self {
self.rejoinable = true;
self
}

- pub fn serialize_into(&self, mut buf: impl BufMut) -> Result<(), BadCodecError> {
+ pub(crate) fn serialize_into(&self, mut buf: impl BufMut) -> Result<(), BadCodecError> {
if buf.remaining_mut() >= 2 {
buf.put_u8(self.id);
buf.put_u8(self.bump);
@@ -52,7 +52,7
}
}

- pub fn deserialize_from(mut buf: impl Buf) -> Result<Self, BadCodecError> {
+ pub(crate) fn deserialize_from(mut buf: impl Buf) -> Result<Self, BadCodecError> {
if buf.remaining() >= 2 {
Ok(Self {
id: buf.get_u8(),
@@ -80,10 +80,10
}
}

-pub struct BadCodec;
+pub(crate) struct BadCodec;

#[derive(Debug, PartialEq, Eq)]
-pub enum BadCodecError {
+pub(crate) enum BadCodecError {
BufTooSmall,
BadMessageID(u8),
BadStateByte(u8),
@@ -339,14 +339,14
}

#[derive(Debug, Clone)]
-pub struct InMemoryRuntime {
+pub(crate) struct InMemoryRuntime {
notifications: Vec<Notification<ID>>,
to_send: Vec<(ID, Bytes)>,
to_schedule: Vec<(Timer<ID>, Duration)>,
}

impl InMemoryRuntime {
- pub fn new() -> Self {
+ pub(crate) fn new() -> Self {
Self {
notifications: Vec::new(),
to_send: Vec::new(),
@@ -354,24 +354,27
}
}

- pub fn clear(&mut self) {
+ pub(crate) fn clear(&mut self) {
self.notifications.clear();
self.to_send.clear();
self.to_schedule.clear();
}

- pub fn take_all_data(&mut self) -> Vec<(ID, Bytes)> {
+ pub(crate) fn take_all_data(&mut self) -> Vec<(ID, Bytes)> {
core::mem::take(&mut self.to_send)
}

- pub fn take_data(&mut self, dst: ID) -> Option<Bytes> {
+ pub(crate) fn take_data(&mut self, dst: ID) -> Option<Bytes> {
let position = self.to_send.iter().position(|(to, _data)| to == &dst)?;

let taken = self.to_send.swap_remove(position);
Some(taken.1)
}

- pub fn take_notification(&mut self, wanted: Notification<ID>) -> Option<Notification<ID>> {
+ pub(crate) fn take_notification(
+ &mut self,
+ wanted: Notification<ID>,
+ ) -> Option<Notification<ID>> {
let position = self
.notifications
.iter()
@@ -381,7 +384,7
Some(taken)
}

- pub fn take_scheduling(&mut self, timer: Timer<ID>) -> Option<Duration> {
+ pub(crate) fn take_scheduling(&mut self, timer: Timer<ID>) -> Option<Duration> {
let position = self
.to_schedule
.iter()
@@ -391,7 +394,7
Some(taken.1)
}

- pub fn find_scheduling<F>(&self, predicate: F) -> Option<&Timer<ID>>
+ pub(crate) fn find_scheduling<F>(&self, predicate: F) -> Option<&Timer<ID>>
where
F: Fn(&Timer<ID>) -> bool,
{
@@ -418,10 +421,10

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
-pub struct TrivialID(u64);
+pub(crate) struct TrivialID(u64);

#[cfg(any(feature = "bincode-codec", feature = "postcard-codec"))]
-pub fn verify_codec_roundtrip<C: Codec<TrivialID>>(mut codec: C) -> Result<(), C::Error> {
+pub(crate) fn verify_codec_roundtrip<C: Codec<TrivialID>>(mut codec: C) -> Result<(), C::Error> {
let mut buf = Vec::new();

let payload = Header {