Skip to content

Commit

Permalink
Merge pull request #117 from greyblake/inline-trait-impls
Browse files Browse the repository at this point in the history
Use #[inline] attr for trivial functions
  • Loading branch information
greyblake committed Jan 6, 2024
2 parents affa79c + 1b63cf7 commit 67effeb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Support `Arbitrary` for integer types
* Support `Arbitrary` for float types
* Ability to specify boundaries (`greater`, `greater_or_equal`, `less`, `less_or_equal`, `len_char_min`, `len_char_max`) with expressions or named constants.
* Add `#[inline]` attribute to trivial functions

### v0.4.0 - 2023-11-21
* Support of arbitrary inner types with custom sanitizers and validators.
Expand Down
1 change: 1 addition & 0 deletions nutype_macros/src/common/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub fn gen_reimports(
pub fn gen_impl_into_inner(type_name: &TypeName, inner_type: impl ToTokens) -> TokenStream {
quote! {
impl #type_name {
#[inline]
pub fn into_inner(self) -> #inner_type {
self.0
}
Expand Down
42 changes: 26 additions & 16 deletions nutype_macros/src/common/gen/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn gen_impl_trait_into(type_name: &TypeName, inner_type: impl Into<InnerType
// From<Type> for Inner
quote! {
impl ::core::convert::From<#type_name> for #inner_type {
#[inline]
fn from(value: #type_name) -> Self {
value.into_inner()
}
Expand All @@ -70,6 +71,7 @@ pub fn gen_impl_trait_into(type_name: &TypeName, inner_type: impl Into<InnerType
pub fn gen_impl_trait_as_ref(type_name: &TypeName, inner_type: impl ToTokens) -> TokenStream {
quote! {
impl ::core::convert::AsRef<#inner_type> for #type_name {
#[inline]
fn as_ref(&self) -> &#inner_type {
&self.0
}
Expand All @@ -82,6 +84,7 @@ pub fn gen_impl_trait_deref(type_name: &TypeName, inner_type: impl ToTokens) ->
impl ::core::ops::Deref for #type_name {
type Target = #inner_type;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
Expand All @@ -92,10 +95,12 @@ pub fn gen_impl_trait_deref(type_name: &TypeName, inner_type: impl ToTokens) ->
pub fn gen_impl_trait_display(type_name: &TypeName) -> TokenStream {
quote! {
impl ::core::fmt::Display for #type_name {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
// A tiny wrapper function with trait boundary that improves error reporting.
// It makes it clear for the end-user that the inner type has to implement Display
// in order to derive display for the newtype.
#[inline]
fn display<T: ::core::fmt::Display>(f: &mut ::core::fmt::Formatter<'_>, val: &T) -> ::core::fmt::Result {
use ::core::fmt::Display;
val.fmt(f)
Expand All @@ -109,6 +114,7 @@ pub fn gen_impl_trait_display(type_name: &TypeName) -> TokenStream {
pub fn gen_impl_trait_borrow(type_name: &TypeName, borrowed_type: impl ToTokens) -> TokenStream {
quote! {
impl ::core::borrow::Borrow<#borrowed_type> for #type_name {
#[inline]
fn borrow(&self) -> &#borrowed_type {
&self.0
}
Expand All @@ -119,6 +125,7 @@ pub fn gen_impl_trait_borrow(type_name: &TypeName, borrowed_type: impl ToTokens)
pub fn gen_impl_trait_from(type_name: &TypeName, inner_type: impl ToTokens) -> TokenStream {
quote! {
impl ::core::convert::From<#inner_type> for #type_name {
#[inline]
fn from(raw_value: #inner_type) -> Self {
Self::new(raw_value)
}
Expand All @@ -139,6 +146,7 @@ pub fn gen_impl_trait_try_from(
impl ::core::convert::TryFrom<#inner_type> for #type_name {
type Error = #error_type_name;

#[inline]
fn try_from(raw_value: #inner_type) -> Result<#type_name, Self::Error> {
Self::new(raw_value)
}
Expand All @@ -152,6 +160,7 @@ pub fn gen_impl_trait_try_from(
impl ::core::convert::TryFrom<#inner_type> for #type_name {
type Error = ::core::convert::Infallible;

#[inline]
fn try_from(raw_value: #inner_type) -> Result<#type_name, Self::Error> {
Ok(Self::new(raw_value))
}
Expand Down Expand Up @@ -290,26 +299,27 @@ pub fn gen_impl_trait_default(
default_value: impl ToTokens,
has_validation: bool,
) -> TokenStream {
let default_implementation = if has_validation {
if has_validation {
let tp = type_name.to_string();
quote!(
Self::new(#default_value)
.unwrap_or_else(|err| {
let tp = #tp;
panic!("\nDefault value for type `{tp}` is invalid.\nERROR: {err:?}\n");
})
impl ::core::default::Default for #type_name {
fn default() -> Self {
Self::new(#default_value)
.unwrap_or_else(|err| {
let tp = #tp;
panic!("\nDefault value for type `{tp}` is invalid.\nERROR: {err:?}\n");
})
}
}
)
} else {
quote!(
Self::new(#default_value)
)
};

quote!(
impl ::core::default::Default for #type_name {
fn default() -> Self {
#default_implementation
impl ::core::default::Default for #type_name {
#[inline]
fn default() -> Self {
Self::new(#default_value)
}
}
}
)
)
}
}
2 changes: 2 additions & 0 deletions nutype_macros/src/string/gen/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ fn gen_impl_from_str(
impl core::str::FromStr for #type_name {
type Err = #error_type_name;

#[inline]
fn from_str(raw_string: &str) -> ::core::result::Result<Self, Self::Err> {
#type_name::new(raw_string)
}
Expand All @@ -222,6 +223,7 @@ fn gen_impl_from_str(
impl core::str::FromStr for #type_name {
type Err = ::core::convert::Infallible;

#[inline]
fn from_str(raw_string: &str) -> ::core::result::Result<Self, Self::Err> {
Ok(#type_name::new(raw_string))
}
Expand Down

0 comments on commit 67effeb

Please sign in to comment.