Skip to content

Commit

Permalink
refactor: add #[must_use] & #[inline] where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
threadexio committed May 4, 2024
1 parent 3c42c40 commit fff4cd9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
2 changes: 2 additions & 0 deletions channels-io/src/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl<T> BaseBuf<T> {
impl<T: AsRef<[u8]>> BaseBuf<T> {
/// Get a reference to the entire buffer.
#[inline]
#[must_use]
pub fn inner(&self) -> &[u8] {
self.inner.as_ref()
}
Expand Down Expand Up @@ -64,6 +65,7 @@ impl<T: AsRef<[u8]>> BaseBuf<T> {
impl<T: AsMut<[u8]>> BaseBuf<T> {
/// Get a mutable reference to the entire buffer.
#[inline]
#[must_use]
pub fn inner_mut(&mut self) -> &mut [u8] {
self.inner.as_mut()
}
Expand Down
1 change: 1 addition & 0 deletions channels/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub type Pair<T, R, W, Sd> = (Sender<T, W, Sd>, Receiver<T, R, Sd>);
/// let received: i32 = rx.recv().await.unwrap();
/// }
/// ```
#[inline]
pub fn channel<T, R, W>(
r: impl io::IntoRead<R>,
w: impl io::IntoWrite<W>,
Expand Down
33 changes: 27 additions & 6 deletions channels/src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<T> Receiver<T, (), ()> {
/// .deserializer(deserializer)
/// .build();
/// ```
#[must_use]
#[inline]
pub fn builder() -> Builder<T, (), ()> {
Builder::new()
}
Expand Down Expand Up @@ -68,6 +68,7 @@ impl<T, R> Receiver<T, R, crate::serdes::Bincode> {
/// ```
///
/// [`Bincode`]: crate::serdes::Bincode
#[inline]
pub fn new(reader: impl IntoRead<R>) -> Self {
Self::with_deserializer(reader, crate::serdes::Bincode::new())
}
Expand Down Expand Up @@ -104,6 +105,7 @@ impl<T, R, D> Receiver<T, R, D> {
/// deserializer
/// );
/// ```
#[inline]
pub fn with_deserializer(
reader: impl IntoRead<R>,
deserializer: D,
Expand Down Expand Up @@ -136,6 +138,7 @@ impl<T, R, D> Receiver<T, R, D> {
/// println!("{:#?}", rx.config());
///
/// ```
#[inline]
pub fn config(&self) -> &Config {
&self.config
}
Expand Down Expand Up @@ -181,6 +184,7 @@ impl<T, R, D> Receiver<T, R, D> {
/// }
/// }
/// ```
#[inline]
pub fn incoming(&mut self) -> Incoming<'_, T, R, D> {
Incoming { receiver: self }
}
Expand All @@ -198,6 +202,7 @@ impl<T, R, D> Receiver<T, R, D> {
/// assert_eq!(stats.packets(), 0);
/// assert_eq!(stats.ops(), 0);
/// ```
#[inline]
#[cfg(feature = "statistics")]
pub fn statistics(&self) -> &Statistics {
&self.reader.statistics
Expand Down Expand Up @@ -231,6 +236,7 @@ where
/// let r: &MyReader = rx.get();
/// assert_eq!(r.count, 42);
/// ```
#[inline]
pub fn get(&self) -> &R::Inner {
self.reader.inner.get_ref()
}
Expand Down Expand Up @@ -260,11 +266,13 @@ where
/// r.count += 10;
/// assert_eq!(r.count, 52);
/// ```
#[inline]
pub fn get_mut(&mut self) -> &mut R::Inner {
self.reader.inner.get_mut()
}

/// Destruct the receiver and get back the underlying reader.
#[inline]
pub fn into_reader(self) -> R::Inner {
self.reader.inner.into_inner()
}
Expand Down Expand Up @@ -395,6 +403,7 @@ where

/// A builder for [`Receiver`].
#[derive(Clone)]
#[must_use = "builders don't do anything unless you `.build()` them"]
pub struct Builder<T, R, D> {
_marker: PhantomData<fn() -> T>,
reader: R,
Expand All @@ -416,7 +425,7 @@ impl<T> Builder<T, (), ()> {
/// .deserializer(deserializer)
/// .build();
/// ```
#[must_use]
#[inline]
pub fn new() -> Self {
Builder {
_marker: PhantomData,
Expand All @@ -428,6 +437,7 @@ impl<T> Builder<T, (), ()> {
}

impl<T> Default for Builder<T, (), ()> {
#[inline]
fn default() -> Self {
Self::new()
}
Expand All @@ -453,6 +463,7 @@ impl<T, D> Builder<T, (), D> {
/// let builder = channels::Receiver::<i32, _, _>::builder()
/// .reader(tokio::io::empty());
/// ```
#[inline]
pub fn reader<R>(
self,
reader: impl IntoRead<R>,
Expand All @@ -477,6 +488,7 @@ impl<T, R> Builder<T, R, ()> {
/// let builder = channels::Receiver::<i32, _, _>::builder()
/// .deserializer(deserializer);
/// ```
#[inline]
pub fn deserializer<D>(
self,
deserializer: D,
Expand Down Expand Up @@ -504,7 +516,7 @@ impl<T, R, D> Builder<T, R, D> {
/// let rx = Receiver::<i32, _, _>::builder()
/// .config(config);
/// ```
#[must_use]
#[inline]
pub fn config(mut self, config: Config) -> Self {
self.config = Some(config);
self
Expand All @@ -520,6 +532,7 @@ impl<T, R, D> Builder<T, R, D> {
/// .deserializer(channels::serdes::Bincode::new())
/// .build();
/// ```
#[inline]
pub fn build(self) -> Receiver<T, R, D> {
Receiver {
_marker: PhantomData,
Expand Down Expand Up @@ -620,13 +633,15 @@ impl<T, R, D> Builder<T, R, D> {
/// [`Sender`]: crate::Sender
/// [`use_header_checksum()`]: crate::sender::Config::use_header_checksum()
#[derive(Clone)]
#[must_use = "`Config`s don't do anything on their own"]
pub struct Config {
pub(crate) size_estimate: Option<NonZeroUsize>,
pub(crate) max_size: Option<NonZeroUsize>,
pub(crate) verify_header_checksum: bool,
}

impl Default for Config {
#[inline]
fn default() -> Self {
Self {
size_estimate: None,
Expand All @@ -641,13 +656,15 @@ impl Default for Config {

impl Config {
/// Get the size estimate of the [`Receiver`].
#[inline]
#[must_use]
pub fn size_estimate(&self) -> usize {
self.size_estimate.map_or(0, NonZeroUsize::get)
}

/// Set the size estimate of the [`Receiver`].
#[allow(clippy::missing_panics_doc)]
#[inline]
pub fn set_size_estimate(
&mut self,
estimate: usize,
Expand All @@ -663,20 +680,22 @@ impl Config {
}

/// Set the size estimate of the [`Receiver`].
#[must_use]
#[inline]
pub fn with_size_estimate(mut self, estimate: usize) -> Self {
self.set_size_estimate(estimate);
self
}

/// Get the max size of the [`Receiver`].
#[inline]
#[must_use]
pub fn max_size(&self) -> usize {
self.max_size.map_or(0, NonZeroUsize::get)
}

/// Set the max size of the [`Receiver`].
#[allow(clippy::missing_panics_doc)]
#[inline]
pub fn set_max_size(&mut self, max_size: usize) -> &mut Self {
self.max_size = match max_size {
0 => None,
Expand All @@ -689,20 +708,22 @@ impl Config {
}

/// Set the max size of the [`Receiver`].
#[must_use]
#[inline]
pub fn with_max_size(mut self, max_size: usize) -> Self {
self.set_max_size(max_size);
self
}

/// Get whether the [`Receiver`] will verify each packet's header with the
/// checksum.
#[inline]
#[must_use]
pub fn verify_header_checksum(&self) -> bool {
self.verify_header_checksum
}

/// Whether to verify each packet's header with the checksum.
#[inline]
pub fn set_verify_header_checksum(
&mut self,
yes: bool,
Expand All @@ -712,7 +733,7 @@ impl Config {
}

/// Whether to verify each packet's header with the checksum.
#[must_use]
#[inline]
pub fn with_verify_header_checksum(mut self, yes: bool) -> Self {
self.set_verify_header_checksum(yes);
self
Expand Down
Loading

0 comments on commit fff4cd9

Please sign in to comment.