ord_by_key

Crates.ioord_by_key
lib.rsord_by_key
version0.1.3
sourcesrc
created_at2020-11-16 08:28:28.283846
updated_at2020-11-17 08:54:49.747407
descriptionProvides a convenient macro for implementing Ord trait with logic specified in an inline expression
homepagehttps://github.com/pinespear/rust_ord_by_key
repositoryhttps://github.com/pinespear/rust_ord_by_key
max_upload_size
id312822
size29,601
(pinespear)

documentation

README

ord_by_key

Provides a convenient macro for implementing Ord trait with logic specified in an inline expression

use core::cmp::Reverse;
// `Person` will be ordered by `last_name`, then by 
// `first_name`, then by `age` in reverse
#[ord_eq_by_key_selector(|p|
    &p.last_name,
    &p.first_name,
    Reverse(p.age),)]
pub struct Person {
    pub first_name: String,
    pub last_name: String,
    pub age: usize,
}
// Container for `&str` which will be ordered by underlying
// string length
#[ord_eq_by_key_selector(|(s)| s.len())]
pub struct StrByLen<'a>(&'a str);

assert!(StrByLen("Alex") > StrByLen("Bob"));

no_std support

ord_by_key should be compatible with no_std, but it was not tested.

License

Distributed under the terms of both the MIT license and the Apache License (Version 2.0)

Commit count: 11

cargo fmt