with_id/
lib.rs

1#[cfg(feature = "derive")]
2pub use with_id_derive::*;
3
4
5pub trait WithStringId{
6    fn id(&self) -> String;
7}
8
9pub trait WithRefId<T: ?Sized>{
10    fn id(&self) -> &T;
11}
12
13pub trait WithId<T> where T:Clone{
14    fn id(&self) -> T;
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    pub struct Test{
22        pub id:String
23    }
24
25    impl WithId<String> for Test{
26        fn id(&self) -> String {
27            self.id.to_string()
28        }
29    }
30
31
32    pub struct Test2{
33        pub id:String,
34        pub other: i64
35    }
36
37    impl WithRefId<str> for Test2{
38        fn id(&self) -> &str {
39          self.id.as_str()
40        }
41    }
42
43
44
45    #[test]
46    fn it_works() {
47        let test = Test{
48            id:"im-an-id".to_string()
49        };
50        assert_eq!(test.id(), test.id);
51
52        let test = Test2{
53            id:"im-an-id".to_string(),
54            other: 4i64,
55        };
56        assert_eq!(test.id(), test.id);
57    }
58}