Remove into_bytes from impl_array_newtype

All the types that we define with `impl_array_newtype` are
`Copy` so the correct conversion method to get the underlying byte array
is `to_bytes`. We currently provide `into_bytes` as well as `to_bytes`,
with one of them calling `clone` - this is unnecessary and against
convention.

- Remove `into_bytes` and for `to_bytes` just return the inner field.
- Add a method that causes build to fail if `Copy` is not implemented.
This commit is contained in:
Tobin C. Harding 2022-10-26 12:51:26 +11:00
parent bd80ecd92d
commit 8c4fa5ec3d
1 changed files with 8 additions and 5 deletions

View File

@ -32,13 +32,16 @@ macro_rules! impl_array_newtype {
#[inline] #[inline]
pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 } pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 }
/// Returns a clone of the underlying bytes. /// Returns the underlying bytes.
#[inline] #[inline]
pub fn to_bytes(self) -> [$ty; $len] { self.0.clone() } pub fn to_bytes(self) -> [$ty; $len] {
// We rely on `Copy` being implemented for $thing so conversion
// methods use the correct Rust naming conventions.
fn check_copy<T: Copy>() {}
check_copy::<$thing>();
/// Returns the underlying bytes (takes ownership). self.0
#[inline] }
pub fn into_bytes(self) -> [$ty; $len] { self.0 }
} }
impl<'a> core::convert::From<&'a [$ty]> for $thing { impl<'a> core::convert::From<&'a [$ty]> for $thing {