-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conversion attribute to optionally implement From/Into underlying…
… type (#62)
- Loading branch information
1 parent
8d6af97
commit 3f0a3b2
Showing
7 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_conversion(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl From<#value_type> for #name { | ||
fn from(value: #value_type) -> Self { | ||
Self::new(value) | ||
} | ||
} | ||
|
||
impl From<#name> for #value_type { | ||
fn from(value: #name) -> #value_type { | ||
value.0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn implement_str_conversion(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl From<&str> for #name { | ||
fn from(value: &str) -> Self { | ||
Self::new(value) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use std::fmt::Debug; | ||
use strong_type::StrongType; | ||
|
||
fn test<T: Debug + Eq>(value: T, underlying: T) { | ||
assert_eq!(value, underlying); | ||
} | ||
|
||
#[test] | ||
fn test_conversion() { | ||
#[derive(StrongType)] | ||
#[strong_type(conversion)] | ||
struct NamedI32(i64); | ||
|
||
let i64_value = 64; | ||
let value = NamedI32::new(i64_value); | ||
|
||
test(value, i64_value.into()); | ||
test(value.into(), i64_value); | ||
|
||
#[derive(StrongType)] | ||
#[strong_type(conversion)] | ||
struct NamedString(String); | ||
let str_value = "test"; | ||
let string_value = String::from(str_value); | ||
let value = NamedString::new(string_value.clone()); | ||
|
||
test(value.clone(), str_value.into()); | ||
test(value.clone(), string_value.clone().into()); | ||
test(value.into(), string_value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod auto_operators; | ||
mod conversion; | ||
mod custom_underlying; | ||
mod display; | ||
mod strong_type; |