Skip to content

Make trait methods #[inline] #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::missing_safety_doc)]

//! Safely cast bytes slices from/to slices of built-in fundamental numeric types.
//!
Expand Down Expand Up @@ -192,6 +193,7 @@ macro_rules! impl_trait(
}

unsafe impl FromByteSlice for $to {
#[inline]
fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[$to], Error> {
let slice = slice.as_ref();
let len = check_constraints::<$to>(slice)?;
Expand All @@ -208,6 +210,7 @@ macro_rules! impl_trait(
}
}

#[inline]
fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>(slice: &mut T) -> Result<&mut [$to], Error> {
let slice = slice.as_mut();
let len = check_constraints::<$to>(slice)?;
Expand All @@ -226,6 +229,7 @@ macro_rules! impl_trait(
}

unsafe impl ToByteSlice for $to {
#[inline]
fn to_byte_slice<T: AsRef<[$to]> + ?Sized>(slice: &T) -> &[u8] {
let slice = slice.as_ref();
let len = slice.len() * mem::size_of::<$to>();
Expand All @@ -236,6 +240,7 @@ macro_rules! impl_trait(
}

unsafe impl ToMutByteSlice for $to {
#[inline]
fn to_mut_byte_slice<T: AsMut<[$to]> + ?Sized>(slice: &mut T) -> &mut [u8] {
let slice = slice.as_mut();
let len = slice.len() * mem::size_of::<$to>();
Expand All @@ -254,6 +259,7 @@ macro_rules! impl_trait_array (
}

unsafe impl<const N: usize> FromByteSlice for [$to; N] {
#[inline]
fn from_byte_slice<T: AsRef<[u8]> + ?Sized>(slice: &T) -> Result<&[[$to; N]], Error> {
let slice = slice.as_ref();
let len = check_constraints::<[$to; N]>(slice)?;
Expand All @@ -270,6 +276,7 @@ macro_rules! impl_trait_array (
}
}

#[inline]
fn from_mut_byte_slice<T: AsMut<[u8]> + ?Sized>(slice: &mut T) -> Result<&mut [[$to; N]], Error> {
let slice = slice.as_mut();
let len = check_constraints::<[$to; N]>(slice)?;
Expand All @@ -288,6 +295,7 @@ macro_rules! impl_trait_array (
}

unsafe impl<const N: usize> ToByteSlice for [$to; N] {
#[inline]
fn to_byte_slice<T: AsRef<[[$to; N]]> + ?Sized>(slice: &T) -> &[u8] {
let slice = slice.as_ref();
let len = slice.len() * mem::size_of::<[$to; N]>();
Expand All @@ -298,6 +306,7 @@ macro_rules! impl_trait_array (
}

unsafe impl<const N: usize> ToMutByteSlice for [$to; N] {
#[inline]
fn to_mut_byte_slice<T: AsMut<[[$to; N]]> + ?Sized>(slice: &mut T) -> &mut [u8] {
let slice = slice.as_mut();
let len = slice.len() * mem::size_of::<[$to; N]>();
Expand Down Expand Up @@ -380,6 +389,7 @@ pub trait AsSliceOf {
}

impl<U: AsRef<[u8]> + ?Sized> AsSliceOf for U {
#[inline]
fn as_slice_of<T: FromByteSlice>(&self) -> Result<&[T], Error> {
FromByteSlice::from_byte_slice(self)
}
Expand Down Expand Up @@ -409,6 +419,7 @@ pub trait AsMutSliceOf {
}

impl<U: AsMut<[u8]> + ?Sized> AsMutSliceOf for U {
#[inline]
fn as_mut_slice_of<T: FromByteSlice>(&mut self) -> Result<&mut [T], Error> {
FromByteSlice::from_mut_byte_slice(self)
}
Expand Down Expand Up @@ -438,6 +449,7 @@ pub trait AsByteSlice<T> {
}

impl<T: ToByteSlice, U: AsRef<[T]> + ?Sized> AsByteSlice<T> for U {
#[inline]
fn as_byte_slice(&self) -> &[u8] {
ToByteSlice::to_byte_slice(self)
}
Expand Down Expand Up @@ -467,6 +479,7 @@ pub trait AsMutByteSlice<T> {
}

impl<T: ToMutByteSlice, U: AsMut<[T]> + ?Sized> AsMutByteSlice<T> for U {
#[inline]
fn as_mut_byte_slice(&mut self) -> &mut [u8] {
ToMutByteSlice::to_mut_byte_slice(self)
}
Expand Down Expand Up @@ -507,12 +520,14 @@ impl TypeName for () {
}

unsafe impl ToByteSlice for () {
#[inline]
fn to_byte_slice<T: AsRef<[()]> + ?Sized>(_: &T) -> &[u8] {
&[]
}
}

unsafe impl ToMutByteSlice for () {
#[inline]
fn to_mut_byte_slice<T: AsMut<[()]> + ?Sized>(_: &mut T) -> &mut [u8] {
&mut []
}
Expand Down