Skip to content

Commit

Permalink
Add support to sort by tuple index ( #[sort_by(3, 2,1 )] ) (#17)
Browse files Browse the repository at this point in the history
* port sort_by to syn2

* add support for sort_by with tuple indexes

* mention support for anonymous fields in documentation
  • Loading branch information
valsteen authored Aug 6, 2023
1 parent 5fb698e commit ef397b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/enum_variant_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl SomeEnum {
let output = rust_format::RustFmt::default().format_str(output.to_string()).unwrap();
assert_eq!(
output,
r"#[allow(dead_code)]
r#"#[allow(dead_code)]
impl SomeEnum {
pub fn inner_mut(&mut self) -> std::option::Option<&mut usize> {
match self {
Expand All @@ -626,7 +626,7 @@ impl SomeEnum {
}
}
}
"
"#
);
}
}
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,16 @@ mod sort_by;
/// assert_eq!(Something{a: 2, b: 0}.cmp(&Something{a: 1, b: 1}), Ordering::Greater); // a is compared
/// assert_eq!(Something{a: 1, b: 0}.cmp(&Something{a: 1, b: 1}), Ordering::Equal); // b is ignored
/// ```
/// You can use it the same way with tuple structs:
/// You can use it the same way with tuple structs. Tuple members can be referenced by their index in the struct-level
/// `#[sort_by(...)]` attribute.
///
/// ```rust
/// # use std::cmp::Ordering;
/// # use sort_by_derive::SortBy;
/// #
/// #[derive(SortBy)]
/// #[sort_by(0)]
/// struct Something(
/// #[sort_by]
/// u16,
/// #[sort_by]
/// u32,
Expand Down
24 changes: 18 additions & 6 deletions src/sort_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub fn impl_sort_by_derive(input: DeriveInput) -> TokenStream {
}
}

#[allow(clippy::needless_borrow)]
impl #generics core::cmp::Ord for #struct_name <#(#generics_params),*> #where_clause {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
#ord_statement
Expand Down Expand Up @@ -189,14 +190,19 @@ fn parse_outer(attr: Attribute) -> Result<Vec<TokenStream>, Error> {
sortable_fields.last_mut().unwrap().append(token_tree);
(false, false, false)
}
TokenTree::Literal(lit) => {
if let Expr::Lit(ExprLit { lit: Lit::Str(s), .. }) = syn::parse2::<Expr>(lit.into_token_stream())? {
TokenTree::Literal(lit) => match syn::parse2::<Expr>(lit.into_token_stream())? {
Expr::Lit(ExprLit { lit: Lit::Str(s), .. }) => {
sortable_fields.push(s.parse::<Expr>()?.to_token_stream());
(false, false, false)
} else {
}
Expr::Lit(ExprLit { lit: Lit::Int(i), .. }) => {
sortable_fields.push(i.to_token_stream());
(false, false, false)
}
_ => {
return Err(Error::new(span, "invalid expression"));
}
}
},
TokenTree::Ident(_) if dot_provided => {
sortable_fields
.last_mut()
Expand Down Expand Up @@ -258,6 +264,7 @@ impl core::cmp::PartialOrd<Self> for Toto {
std::option::Option::Some(self.cmp(other))
}
}
#[allow(clippy::needless_borrow)]
impl core::cmp::Ord for Toto {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
core::cmp::Ord::cmp(&self.embed.otherfield, &other.embed.otherfield)
Expand Down Expand Up @@ -304,6 +311,7 @@ impl core::cmp::PartialOrd<Self> for Toto {
std::option::Option::Some(self.cmp(other))
}
}
#[allow(clippy::needless_borrow)]
impl core::cmp::Ord for Toto {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
core::cmp::Ord::cmp(&self.this, &other.this)
Expand Down Expand Up @@ -348,6 +356,7 @@ impl core::cmp::PartialOrd<Self> for Toto {
std::option::Option::Some(self.cmp(other))
}
}
#[allow(clippy::needless_borrow)]
impl core::cmp::Ord for Toto {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
core::cmp::Ord::cmp(&self.get_something(), &other.get_something())
Expand Down Expand Up @@ -399,6 +408,7 @@ where
std::option::Option::Some(self.cmp(other))
}
}
#[allow(clippy::needless_borrow)]
impl<'a, T> core::cmp::Ord for ContextWrapper<'a, T>
where
T: Ctx,
Expand All @@ -410,11 +420,10 @@ where
"#
);
}

#[test]
fn test_tuple_struct() {
let input = syn::parse_quote! {
#[sort_by(somemethod(), literal, some.path)]
#[sort_by(somemethod(), literal, some.path, 2)]
struct Something (
#[sort_by]
u16,
Expand All @@ -433,6 +442,7 @@ where
self.somemethod().hash(state);
self.literal.hash(state);
self.some.path.hash(state);
self.2.hash(state);
self.0.hash(state);
self.1.hash(state);
}
Expand All @@ -448,11 +458,13 @@ impl core::cmp::PartialOrd<Self> for Something {
std::option::Option::Some(self.cmp(other))
}
}
#[allow(clippy::needless_borrow)]
impl core::cmp::Ord for Something {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
core::cmp::Ord::cmp(&self.somemethod(), &other.somemethod())
.then_with(|| self.literal.cmp(&other.literal))
.then_with(|| self.some.path.cmp(&other.some.path))
.then_with(|| self.2.cmp(&other.2))
.then_with(|| self.0.cmp(&other.0))
.then_with(|| self.1.cmp(&other.1))
}
Expand Down

0 comments on commit ef397b8

Please sign in to comment.