Refer to `Script{Buf}` as `Self` where relevant

Using `Self` instead of specific type name can make some refactorings
easier.
This commit is contained in:
Martin Habovstiak 2025-02-20 19:05:56 +01:00
parent ce55dd5b70
commit 5680b4e870
3 changed files with 11 additions and 11 deletions

View File

@ -74,28 +74,28 @@ impl ToOwned for Script {
impl Script { impl Script {
/// Constructs a new empty script. /// Constructs a new empty script.
#[inline] #[inline]
pub const fn new() -> &'static Script { Script::from_bytes(&[]) } pub const fn new() -> &'static Self { Self::from_bytes(&[]) }
/// Treat byte slice as `Script` /// Treat byte slice as `Script`
#[inline] #[inline]
pub const fn from_bytes(bytes: &[u8]) -> &Script { pub const fn from_bytes(bytes: &[u8]) -> &Self {
// SAFETY: copied from `std` // SAFETY: copied from `std`
// The pointer was just created from a reference which is still alive. // 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 // Casting slice pointer to a transparent struct wrapping that slice is sound (same
// layout). // layout).
unsafe { &*(bytes as *const [u8] as *const Script) } unsafe { &*(bytes as *const [u8] as *const Self) }
} }
/// Treat mutable byte slice as `Script` /// Treat mutable byte slice as `Script`
#[inline] #[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` // SAFETY: copied from `std`
// The pointer was just created from a reference which is still alive. // 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 // Casting slice pointer to a transparent struct wrapping that slice is sound (same
// layout). // layout).
// Function signature prevents callers from accessing `bytes` while the returned reference // Function signature prevents callers from accessing `bytes` while the returned reference
// is alive. // 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. /// Returns the script data as a byte slice.

View File

@ -299,7 +299,7 @@ impl From<ScriptBuf> for Vec<u8> {
impl AsRef<Script> for Script { impl AsRef<Script> for Script {
#[inline] #[inline]
fn as_ref(&self) -> &Script { self } fn as_ref(&self) -> &Self { self }
} }
impl AsRef<Script> for ScriptBuf { impl AsRef<Script> for ScriptBuf {
@ -319,7 +319,7 @@ impl AsRef<[u8]> for ScriptBuf {
impl AsMut<Script> for Script { impl AsMut<Script> for Script {
#[inline] #[inline]
fn as_mut(&mut self) -> &mut Script { self } fn as_mut(&mut self) -> &mut Self { self }
} }
impl AsMut<Script> for ScriptBuf { impl AsMut<Script> for ScriptBuf {
@ -435,14 +435,14 @@ impl fmt::LowerHex for Script {
} }
} }
#[cfg(feature = "alloc")] #[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 { impl fmt::LowerHex for ScriptBuf {
#[inline] #[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self.as_script(), f) } fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self.as_script(), f) }
} }
#[cfg(feature = "alloc")] #[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 { impl fmt::UpperHex for Script {
#[inline] #[inline]

View File

@ -23,13 +23,13 @@ pub struct ScriptBuf(Vec<u8>);
impl ScriptBuf { impl ScriptBuf {
/// Constructs a new empty script. /// Constructs a new empty script.
#[inline] #[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. /// Converts byte vector into script.
/// ///
/// This method doesn't (re)allocate. /// This method doesn't (re)allocate.
#[inline] #[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. /// Returns a reference to unsized script.
#[inline] #[inline]