Skip to content

Commit

Permalink
Ensure that various Into/From ABI methods are inlined
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed Sep 11, 2019
1 parent 908fc61 commit 68af85d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/convert/impls.rs
Expand Up @@ -275,6 +275,7 @@ impl OptionFromWasmAbi for char {
impl<T> IntoWasmAbi for *const T {
type Abi = u32;

#[inline]
fn into_abi(self) -> u32 {
self as u32
}
Expand All @@ -283,6 +284,7 @@ impl<T> IntoWasmAbi for *const T {
impl<T> FromWasmAbi for *const T {
type Abi = u32;

#[inline]
unsafe fn from_abi(js: u32) -> *const T {
js as *const T
}
Expand All @@ -291,6 +293,7 @@ impl<T> FromWasmAbi for *const T {
impl<T> IntoWasmAbi for *mut T {
type Abi = u32;

#[inline]
fn into_abi(self) -> u32 {
self as u32
}
Expand All @@ -299,6 +302,7 @@ impl<T> IntoWasmAbi for *mut T {
impl<T> FromWasmAbi for *mut T {
type Abi = u32;

#[inline]
unsafe fn from_abi(js: u32) -> *mut T {
js as *mut T
}
Expand Down Expand Up @@ -346,6 +350,7 @@ impl RefFromWasmAbi for JsValue {
impl<T: OptionIntoWasmAbi> IntoWasmAbi for Option<T> {
type Abi = T::Abi;

#[inline]
fn into_abi(self) -> T::Abi {
match self {
None => T::none(),
Expand All @@ -357,6 +362,7 @@ impl<T: OptionIntoWasmAbi> IntoWasmAbi for Option<T> {
impl<T: OptionFromWasmAbi> FromWasmAbi for Option<T> {
type Abi = T::Abi;

#[inline]
unsafe fn from_abi(js: T::Abi) -> Self {
if T::is_none(&js) {
None
Expand All @@ -369,6 +375,7 @@ impl<T: OptionFromWasmAbi> FromWasmAbi for Option<T> {
impl<T: IntoWasmAbi> IntoWasmAbi for Clamped<T> {
type Abi = T::Abi;

#[inline]
fn into_abi(self) -> Self::Abi {
self.0.into_abi()
}
Expand All @@ -377,6 +384,7 @@ impl<T: IntoWasmAbi> IntoWasmAbi for Clamped<T> {
impl<T: FromWasmAbi> FromWasmAbi for Clamped<T> {
type Abi = T::Abi;

#[inline]
unsafe fn from_abi(js: T::Abi) -> Self {
Clamped(T::from_abi(js))
}
Expand All @@ -394,6 +402,7 @@ impl IntoWasmAbi for () {
impl<T: IntoWasmAbi> ReturnWasmAbi for Result<T, JsValue> {
type Abi = T::Abi;

#[inline]
fn return_abi(self) -> Self::Abi {
match self {
Ok(v) => v.into_abi(),
Expand Down
13 changes: 13 additions & 0 deletions src/convert/slices.rs
Expand Up @@ -45,6 +45,7 @@ macro_rules! vectors {
}

impl OptionIntoWasmAbi for Box<[$t]> {
#[inline]
fn none() -> WasmSlice { null_slice() }
}

Expand All @@ -60,6 +61,7 @@ macro_rules! vectors {
}

impl OptionFromWasmAbi for Box<[$t]> {
#[inline]
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
}
}
Expand All @@ -77,6 +79,7 @@ macro_rules! vectors {
}

impl<'a> OptionIntoWasmAbi for &'a [$t] {
#[inline]
fn none() -> WasmSlice { null_slice() }
}

Expand All @@ -90,6 +93,7 @@ macro_rules! vectors {
}

impl<'a> OptionIntoWasmAbi for &'a mut [$t] {
#[inline]
fn none() -> WasmSlice { null_slice() }
}

Expand Down Expand Up @@ -144,24 +148,28 @@ if_std! {
impl<T> IntoWasmAbi for Vec<T> where Box<[T]>: IntoWasmAbi<Abi = WasmSlice> {
type Abi = <Box<[T]> as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self) -> Self::Abi {
self.into_boxed_slice().into_abi()
}
}

impl<T> OptionIntoWasmAbi for Vec<T> where Box<[T]>: IntoWasmAbi<Abi = WasmSlice> {
#[inline]
fn none() -> WasmSlice { null_slice() }
}

impl<T> FromWasmAbi for Vec<T> where Box<[T]>: FromWasmAbi<Abi = WasmSlice> {
type Abi = <Box<[T]> as FromWasmAbi>::Abi;

#[inline]
unsafe fn from_abi(js: Self::Abi) -> Self {
<Box<[T]>>::from_abi(js).into()
}
}

impl<T> OptionFromWasmAbi for Vec<T> where Box<[T]>: FromWasmAbi<Abi = WasmSlice> {
#[inline]
fn is_none(abi: &WasmSlice) -> bool { abi.ptr == 0 }
}

Expand All @@ -177,6 +185,7 @@ if_std! {
}

impl OptionIntoWasmAbi for String {
#[inline]
fn none() -> Self::Abi { null_slice() }
}

Expand All @@ -190,6 +199,7 @@ if_std! {
}

impl OptionFromWasmAbi for String {
#[inline]
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
}
}
Expand All @@ -206,6 +216,7 @@ impl<'a> IntoWasmAbi for &'a str {
}

impl<'a> OptionIntoWasmAbi for &'a str {
#[inline]
fn none() -> Self::Abi {
null_slice()
}
Expand Down Expand Up @@ -240,6 +251,7 @@ if_std! {
}

impl OptionIntoWasmAbi for Box<[JsValue]> {
#[inline]
fn none() -> WasmSlice { null_slice() }
}

Expand All @@ -255,6 +267,7 @@ if_std! {
}

impl OptionFromWasmAbi for Box<[JsValue]> {
#[inline]
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
}
}
2 changes: 2 additions & 0 deletions src/convert/traits.rs
Expand Up @@ -121,6 +121,8 @@ pub trait ReturnWasmAbi: WasmDescribe {

impl<T: IntoWasmAbi> ReturnWasmAbi for T {
type Abi = T::Abi;

#[inline]
fn return_abi(self) -> Self::Abi {
self.into_abi()
}
Expand Down

0 comments on commit 68af85d

Please sign in to comment.