script: Re-order functions
We want our code to be easy to read and our APIs discoverable, for those of us who read source files the layout matters. Put the constructors and getters at the top of the impl block. Code move only, no logic changes.
This commit is contained in:
parent
db40297f87
commit
3af3239ad0
|
@ -97,14 +97,6 @@ impl Script {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_mut_bytes(&mut self) -> &mut [u8] { &mut self.0 }
|
pub fn as_mut_bytes(&mut self) -> &mut [u8] { &mut self.0 }
|
||||||
|
|
||||||
/// Returns the length in bytes of the script.
|
|
||||||
#[inline]
|
|
||||||
pub fn len(&self) -> usize { self.0.len() }
|
|
||||||
|
|
||||||
/// Returns whether the script is the empty script.
|
|
||||||
#[inline]
|
|
||||||
pub fn is_empty(&self) -> bool { self.0.is_empty() }
|
|
||||||
|
|
||||||
/// Returns a copy of the script data.
|
/// Returns a copy of the script data.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_vec(&self) -> Vec<u8> { self.0.to_owned() }
|
pub fn to_vec(&self) -> Vec<u8> { self.0.to_owned() }
|
||||||
|
@ -114,6 +106,14 @@ impl Script {
|
||||||
#[deprecated(since = "TBD", note = "use to_vec instead")]
|
#[deprecated(since = "TBD", note = "use to_vec instead")]
|
||||||
pub fn to_bytes(&self) -> Vec<u8> { self.to_vec() }
|
pub fn to_bytes(&self) -> Vec<u8> { self.to_vec() }
|
||||||
|
|
||||||
|
/// Returns the length in bytes of the script.
|
||||||
|
#[inline]
|
||||||
|
pub fn len(&self) -> usize { self.0.len() }
|
||||||
|
|
||||||
|
/// Returns whether the script is the empty script.
|
||||||
|
#[inline]
|
||||||
|
pub fn is_empty(&self) -> bool { self.0.is_empty() }
|
||||||
|
|
||||||
/// Converts a [`Box<Script>`](Box) into a [`ScriptBuf`] without copying or allocating.
|
/// Converts a [`Box<Script>`](Box) into a [`ScriptBuf`] without copying or allocating.
|
||||||
#[must_use = "`self` will be dropped if the result is not used"]
|
#[must_use = "`self` will be dropped if the result is not used"]
|
||||||
pub fn into_script_buf(self: Box<Self>) -> ScriptBuf {
|
pub fn into_script_buf(self: Box<Self>) -> ScriptBuf {
|
||||||
|
|
|
@ -26,6 +26,36 @@ impl ScriptBuf {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn new() -> Self { ScriptBuf(Vec::new()) }
|
pub const fn new() -> Self { ScriptBuf(Vec::new()) }
|
||||||
|
|
||||||
|
/// Converts byte vector into script.
|
||||||
|
///
|
||||||
|
/// This method doesn't (re)allocate.
|
||||||
|
pub fn from_bytes(bytes: Vec<u8>) -> Self { ScriptBuf(bytes) }
|
||||||
|
|
||||||
|
/// Returns a reference to unsized script.
|
||||||
|
pub fn as_script(&self) -> &Script { Script::from_bytes(&self.0) }
|
||||||
|
|
||||||
|
/// Returns a mutable reference to unsized script.
|
||||||
|
pub fn as_mut_script(&mut self) -> &mut Script { Script::from_bytes_mut(&mut self.0) }
|
||||||
|
|
||||||
|
/// Converts the script into a byte vector.
|
||||||
|
///
|
||||||
|
/// This method doesn't (re)allocate.
|
||||||
|
pub fn into_bytes(self) -> Vec<u8> { self.0 }
|
||||||
|
|
||||||
|
/// Converts this `ScriptBuf` into a [boxed](Box) [`Script`].
|
||||||
|
///
|
||||||
|
/// This method reallocates if the capacity is greater than length of the script but should not
|
||||||
|
/// when they are equal. If you know beforehand that you need to create a script of exact size
|
||||||
|
/// use [`reserve_exact`](Self::reserve_exact) before adding data to the script so that the
|
||||||
|
/// reallocation can be avoided.
|
||||||
|
#[must_use = "`self` will be dropped if the result is not used"]
|
||||||
|
#[inline]
|
||||||
|
pub fn into_boxed_script(self) -> Box<Script> {
|
||||||
|
// Copied from PathBuf::into_boxed_path
|
||||||
|
let rw = Box::into_raw(self.0.into_boxed_slice()) as *mut Script;
|
||||||
|
unsafe { Box::from_raw(rw) }
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a new empty script with pre-allocated capacity.
|
/// Creates a new empty script with pre-allocated capacity.
|
||||||
pub fn with_capacity(capacity: usize) -> Self { ScriptBuf(Vec::with_capacity(capacity)) }
|
pub fn with_capacity(capacity: usize) -> Self { ScriptBuf(Vec::with_capacity(capacity)) }
|
||||||
|
|
||||||
|
@ -55,36 +85,6 @@ impl ScriptBuf {
|
||||||
///
|
///
|
||||||
/// Panics if the new capacity exceeds `isize::MAX bytes`.
|
/// Panics if the new capacity exceeds `isize::MAX bytes`.
|
||||||
pub fn reserve_exact(&mut self, additional_len: usize) { self.0.reserve_exact(additional_len); }
|
pub fn reserve_exact(&mut self, additional_len: usize) { self.0.reserve_exact(additional_len); }
|
||||||
|
|
||||||
/// Returns a reference to unsized script.
|
|
||||||
pub fn as_script(&self) -> &Script { Script::from_bytes(&self.0) }
|
|
||||||
|
|
||||||
/// Returns a mutable reference to unsized script.
|
|
||||||
pub fn as_mut_script(&mut self) -> &mut Script { Script::from_bytes_mut(&mut self.0) }
|
|
||||||
|
|
||||||
/// Converts byte vector into script.
|
|
||||||
///
|
|
||||||
/// This method doesn't (re)allocate.
|
|
||||||
pub fn from_bytes(bytes: Vec<u8>) -> Self { ScriptBuf(bytes) }
|
|
||||||
|
|
||||||
/// Converts the script into a byte vector.
|
|
||||||
///
|
|
||||||
/// This method doesn't (re)allocate.
|
|
||||||
pub fn into_bytes(self) -> Vec<u8> { self.0 }
|
|
||||||
|
|
||||||
/// Converts this `ScriptBuf` into a [boxed](Box) [`Script`].
|
|
||||||
///
|
|
||||||
/// This method reallocates if the capacity is greater than length of the script but should not
|
|
||||||
/// when they are equal. If you know beforehand that you need to create a script of exact size
|
|
||||||
/// use [`reserve_exact`](Self::reserve_exact) before adding data to the script so that the
|
|
||||||
/// reallocation can be avoided.
|
|
||||||
#[must_use = "`self` will be dropped if the result is not used"]
|
|
||||||
#[inline]
|
|
||||||
pub fn into_boxed_script(self) -> Box<Script> {
|
|
||||||
// Copied from PathBuf::into_boxed_path
|
|
||||||
let rw = Box::into_raw(self.0.into_boxed_slice()) as *mut Script;
|
|
||||||
unsafe { Box::from_raw(rw) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "arbitrary")]
|
#[cfg(feature = "arbitrary")]
|
||||||
|
|
Loading…
Reference in New Issue