Skip to content
This repository has been archived by the owner on Feb 5, 2020. It is now read-only.

Ensure sized on associated methods #6

Merged
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions named_type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@
pub trait NamedType {
/// Returns the canonical name with the fully qualified module name for the
/// given type
fn type_name() -> &'static str;
fn type_name() -> &'static str
where
Self: Sized;

/// Returns a user-friendly short name for the given type
fn short_type_name() -> &'static str;
fn short_type_name() -> &'static str
where
Self: Sized;
}
20 changes: 13 additions & 7 deletions named_type_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ fn find_prefix_suffix(props: &Vec<NestedMetaItem>) -> Option<(&str, &str)> {
ident == "short_prefix"
}
_ => false,
}).and_then(|item| match item {
})
.and_then(|item| match item {
&NestedMetaItem::MetaItem(MetaItem::NameValue(_, ref value)) => match value {
&Lit::Str(ref string, _) => Some(string.as_ref()),
_ => None,
},
_ => None,
}).unwrap_or("");
})
.unwrap_or("");

let suffix = props
.iter()
Expand All @@ -49,13 +51,15 @@ fn find_prefix_suffix(props: &Vec<NestedMetaItem>) -> Option<(&str, &str)> {
ident.to_string() == "short_suffix"
}
_ => false,
}).and_then(|item| match item {
})
.and_then(|item| match item {
&NestedMetaItem::MetaItem(MetaItem::NameValue(_, ref value)) => match value {
&Lit::Str(ref string, _) => Some(string.as_ref()),
_ => None,
},
_ => None,
}).unwrap_or("");
})
.unwrap_or("");

Some((prefix, suffix))
}
Expand All @@ -70,15 +74,17 @@ fn named_type_derive(ast: syn::MacroInput) -> quote::Tokens {
.find(|attr| match &attr.value {
&MetaItem::List(ref ident, _) => ident == "named_type",
_ => false,
}).and_then(|attr| match &attr.value {
})
.and_then(|attr| match &attr.value {
&MetaItem::List(_, ref props) => find_prefix_suffix(props),
_ => None,
}).unwrap_or(("", ""))
})
.unwrap_or(("", ""))
};

let short_type_name: String = format!("{}{}{}", prefix, name, suffix);

quote!{
quote! {
impl #impl_generics NamedType for #name #ty_generics #where_clause {
fn type_name() -> &'static str {
concat!(module_path!(), "::", stringify!(#name))
Expand Down
19 changes: 17 additions & 2 deletions named_type_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ fn test_struct() {

#[allow(dead_code)]
#[derive(NamedType)]
enum MyEnum {}
enum MyEnum {
V1
}

#[test]
fn test_enum() {
Expand All @@ -27,7 +29,9 @@ fn test_enum() {
#[allow(dead_code)]
#[derive(NamedType)]
#[named_type(short_prefix = "Pre")]
enum Prefixed {}
enum Prefixed {
V1
}

#[test]
fn test_prefix() {
Expand All @@ -45,3 +49,14 @@ fn test_suffix() {
assert_eq!(Suffixed::type_name(), concat!(module_path!(), "::Suffixed"));
assert_eq!(Suffixed::short_type_name(), "Suffixed_suffix");
}

#[test]
fn test_ensure_that_structs_could_be_made_into_objects() {
let list_of_boxed_named_type: Vec<Box<NamedType>> = vec![
Box::new(MyStruct {}),
Box::new(MyEnum::V1),
Box::new(Prefixed::V1),
Box::new(Suffixed {}),
];
assert_eq!(list_of_boxed_named_type.len(), 4);
}