Skip to content

Commit

Permalink
Merge rust-bitcoin#3855: io: Use get_ref / get_mut API
Browse files Browse the repository at this point in the history
12a83e1 api: Run just check-api (Tobin C. Harding)
88dfd4e io: Use get_ref / get_mut API (Tobin C. Harding)
b7fdb35 io: Move constructors (Tobin C. Harding)

Pull request description:

  Currently in the `bridge` module to get a reference and mutable reference we provide the `as_inner` and `inner_mut` functions. These names are not in line with the `std::io` module which favours `get_ref` and `get_mut`.

  Add inherent functions `get_ref` and `get_mut` for accessing the wrapped value in `bridge::ToStd` and deprecate `as_inner` and `inner_mut`.

  To convince yourself this API is correct grep the stdlib `io` module for `fn get_ref` as opposed to `fn as_ref`.

  Patch 1 is a trivial code move cleanup.

  Close: rust-bitcoin#3832

ACKs for top commit:
  apoelstra:
    ACK 12a83e1; successfully ran local tests

Tree-SHA512: a5ebd806a4914aca050746ffca4930dc0a5bbeffd0d2fdb000af3bebd4d932cb08ec68acd18cd0a8a9cecf3cb7a211e066ebfd2fd62aaf40c5b4b5348e39e6d4
  • Loading branch information
apoelstra committed Jan 6, 2025
2 parents 33d7052 + 12a83e1 commit b0ec566
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 2 additions & 0 deletions api/io/all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ pub fn bitcoin_io::FromStd<T>::fill_buf(&mut self) -> bitcoin_io::Result<&[u8]>
pub fn bitcoin_io::FromStd<T>::fill_buf(&mut self) -> std::io::error::Result<&[u8]>
pub fn bitcoin_io::FromStd<T>::flush(&mut self) -> bitcoin_io::Result<()>
pub fn bitcoin_io::FromStd<T>::flush(&mut self) -> std::io::error::Result<()>
pub fn bitcoin_io::FromStd<T>::get_mut(&mut self) -> &mut T
pub fn bitcoin_io::FromStd<T>::get_ref(&self) -> &T
pub fn bitcoin_io::FromStd<T>::inner(&self) -> &T
pub fn bitcoin_io::FromStd<T>::inner_mut(&mut self) -> &mut T
pub fn bitcoin_io::FromStd<T>::into_inner(self) -> T
Expand Down
34 changes: 22 additions & 12 deletions io/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ impl<T> FromStd<T> {
#[inline]
pub const fn new(inner: T) -> Self { Self(inner) }

/// Returns the wrapped value.
#[inline]
pub fn into_inner(self) -> T { self.0 }

/// Returns a reference to the wrapped value.
#[inline]
pub fn inner(&self) -> &T { &self.0 }

/// Returns a mutable reference to the wrapped value.
#[inline]
pub fn inner_mut(&mut self) -> &mut T { &mut self.0 }

/// Wraps a mutable reference to IO type.
#[inline]
pub fn new_mut(inner: &mut T) -> &mut Self {
Expand All @@ -38,6 +26,28 @@ impl<T> FromStd<T> {
// SAFETY: the type is repr(transparent) and the pointer is created from Box
unsafe { Box::from_raw(Box::into_raw(inner) as *mut Self) }
}

/// Returns the wrapped value.
#[inline]
pub fn into_inner(self) -> T { self.0 }

/// Returns a reference to the wrapped value.
#[inline]
pub fn get_ref(&self) -> &T { &self.0 }

/// Returns a mutable reference to the wrapped value.
#[inline]
pub fn get_mut(&mut self) -> &mut T { &mut self.0 }

/// Returns a reference to the wrapped value.
#[inline]
#[deprecated(since = "TBD", note = "use `get_ref()` instead")]
pub fn inner(&self) -> &T { &self.0 }

/// Returns a mutable reference to the wrapped value.
#[inline]
#[deprecated(since = "TBD", note = "use `get_ref()` instead")]
pub fn inner_mut(&mut self) -> &mut T { &mut self.0 }
}

impl<T: std::io::Read> super::Read for FromStd<T> {
Expand Down

0 comments on commit b0ec566

Please sign in to comment.