Skip to content

Commit

Permalink
Refer to Script{Buf} as Self where relevant
Browse files Browse the repository at this point in the history
Using `Self` instead of specific type name can make some refactorings
easier.
  • Loading branch information
Kixunil committed Feb 20, 2025
1 parent ce55dd5 commit 5680b4e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions primitives/src/script/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,28 @@ impl ToOwned for Script {
impl Script {
/// Constructs a new empty script.
#[inline]
pub const fn new() -> &'static Script { Script::from_bytes(&[]) }
pub const fn new() -> &'static Self { Self::from_bytes(&[]) }

/// Treat byte slice as `Script`
#[inline]
pub const fn from_bytes(bytes: &[u8]) -> &Script {
pub const fn from_bytes(bytes: &[u8]) -> &Self {
// SAFETY: copied from `std`
// The pointer was just created from a reference which is still alive.
// Casting slice pointer to a transparent struct wrapping that slice is sound (same
// layout).
unsafe { &*(bytes as *const [u8] as *const Script) }
unsafe { &*(bytes as *const [u8] as *const Self) }
}

/// Treat mutable byte slice as `Script`
#[inline]
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Script {
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self {
// SAFETY: copied from `std`
// The pointer was just created from a reference which is still alive.
// Casting slice pointer to a transparent struct wrapping that slice is sound (same
// layout).
// Function signature prevents callers from accessing `bytes` while the returned reference
// is alive.
unsafe { &mut *(bytes as *mut [u8] as *mut Script) }
unsafe { &mut *(bytes as *mut [u8] as *mut Self) }
}

/// Returns the script data as a byte slice.
Expand Down
8 changes: 4 additions & 4 deletions primitives/src/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl From<ScriptBuf> for Vec<u8> {

impl AsRef<Script> for Script {
#[inline]
fn as_ref(&self) -> &Script { self }
fn as_ref(&self) -> &Self { self }
}

impl AsRef<Script> for ScriptBuf {
Expand All @@ -319,7 +319,7 @@ impl AsRef<[u8]> for ScriptBuf {

impl AsMut<Script> for Script {
#[inline]
fn as_mut(&mut self) -> &mut Script { self }
fn as_mut(&mut self) -> &mut Self { self }
}

impl AsMut<Script> for ScriptBuf {
Expand Down Expand Up @@ -435,14 +435,14 @@ impl fmt::LowerHex for Script {
}
}
#[cfg(feature = "alloc")]
internals::impl_to_hex_from_lower_hex!(Script, |script: &Script| script.len() * 2);
internals::impl_to_hex_from_lower_hex!(Script, |script: &Self| script.len() * 2);

impl fmt::LowerHex for ScriptBuf {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self.as_script(), f) }
}
#[cfg(feature = "alloc")]
internals::impl_to_hex_from_lower_hex!(ScriptBuf, |script_buf: &ScriptBuf| script_buf.len() * 2);
internals::impl_to_hex_from_lower_hex!(ScriptBuf, |script_buf: &Self| script_buf.len() * 2);

impl fmt::UpperHex for Script {
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/script/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub struct ScriptBuf(Vec<u8>);
impl ScriptBuf {
/// Constructs a new empty script.
#[inline]
pub const fn new() -> Self { ScriptBuf::from_bytes(Vec::new()) }
pub const fn new() -> Self { Self::from_bytes(Vec::new()) }

/// Converts byte vector into script.
///
/// This method doesn't (re)allocate.
#[inline]
pub const fn from_bytes(bytes: Vec<u8>) -> Self { ScriptBuf(bytes) }
pub const fn from_bytes(bytes: Vec<u8>) -> Self { Self(bytes) }

/// Returns a reference to unsized script.
#[inline]
Expand Down

0 comments on commit 5680b4e

Please sign in to comment.