1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! [![pipeline status](https://gitlab.com/mexus/clone-fields/badges/master/pipeline.svg)](https://gitlab.com/mexus/clone-fields/commits/master)
//! [![crates.io](https://img.shields.io/crates/v/clone-fields.svg)](https://crates.io/crates/clone-fields)
//! [![docs.rs](https://docs.rs/clone-fields/badge.svg)](https://docs.rs/clone-fields)
//!
//! [[Release docs]](https://docs.rs/clone-fields/)
//!
//! [[Master docs]](https://mexus.gitlab.io/clone-fields/clone_fields/)
//!
//! Fields-wise types cloning. Nothing more, nothing less.
//!
//! ```
//! use clone_fields::CloneFields;
//!
//! // PartialEq and Debug traits are only required for `assert` macros in the example.
//! #[derive(PartialEq, Debug, CloneFields)]
//! #[clone_fields(External)]
//! struct Original<'a, T: Clone> {
//!     field1: &'a i64,
//!     field2: T,
//!     nested: OriginalNested,
//! }
//!
//! #[derive(PartialEq, Debug, CloneFields)]
//! #[clone_fields(ExternalNested, ExternalNested2)]
//! struct OriginalNested {
//!     value: i32,
//! }
//!
//! // S2 might be a *foreign* type, i.e. declared in a different crate.
//! struct External<'a, T: Clone> {
//!     field1: &'a i64,
//!     field2: T,
//!     nested: ExternalNested,
//! }
//!
//! struct ExternalNested {
//!     value: i32,
//! }
//!
//! // This struct only exists for the sake of using `clone_fields` attribute with more than one
//! // type :)
//! struct ExternalNested2 {
//!     value: i32,
//! }
//!
//! fn main() {
//!     let obj: Original<_> = Original {
//!         field1: &0,
//!         field2: String::from("lol"),
//!         nested: OriginalNested { value: 15 }
//!     };
//!     let cloned: External<_> = obj.clone_into();
//!     assert_eq!(obj.field1, cloned.field1);
//!     assert_eq!(obj.field2, cloned.field2);
//!     assert_eq!(obj.nested.value, cloned.nested.value);
//!     let cloned2: Original<_> = CloneFields::clone_from(&cloned);
//!     assert_eq!(cloned.field1, cloned2.field1);
//!     assert_eq!(cloned.field2, cloned2.field2);
//!     assert_eq!(obj, cloned2);
//! }
//! ```

#![deny(missing_docs)]

extern crate clone_fields_derive;

pub use clone_fields_derive::*;

/// A trait to clone a type into another type field-by-field.
///
/// It is automatically implemented for any clonable type, i.e. `CloneFields<T> for T` where
/// `T: Clone`.
///
/// Refer to [clone-fields-derive](https://docs.rs/clone-fields-derive) docs for info on how to
/// automatically derive this trait. Yes, you typically don't need to do it manually.
pub trait CloneFields<Target> {
    /// Clones `self` into another type by cloning its fields.
    fn clone_into(&self) -> Target;

    /// Constructs `Self` from another type by cloning its fields.
    fn clone_from(other: &Target) -> Self;
}

impl<T> CloneFields<T> for T
where
    T: Clone,
{
    fn clone_into(&self) -> T {
        self.clone()
    }

    fn clone_from(other: &T) -> Self {
        other.clone()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[derive(Debug, PartialEq, CloneFields)]
    #[clone_fields(S2)]
    struct S1<'a, T: Clone> {
        field1: &'a i64,
        field2: T,
        field3: Inner1,
    }

    #[derive(Debug)]
    struct S2<'a, T: Clone> {
        field1: &'a i64,
        field2: T,
        field3: Inner2,
    }

    impl<'a, T> PartialEq<S2<'a, T>> for S1<'a, T>
    where
        T: PartialEq + Clone,
    {
        fn eq(&self, other: &S2<'a, T>) -> bool {
            self.field1 == other.field1
                && self.field2 == other.field2
                && self.field3 == other.field3
        }
    }

    #[derive(Debug, PartialEq, CloneFields)]
    #[clone_fields(Inner2)]
    struct Inner1 {
        x: i32,
    }

    #[derive(Debug, PartialEq)]
    struct Inner2 {
        x: i32,
    }

    impl PartialEq<Inner2> for Inner1 {
        fn eq(&self, other: &Inner2) -> bool {
            self.x == other.x
        }
    }

    #[test]
    fn check() {
        let original = S1 {
            field1: &10,
            field2: "lol".to_string(),
            field3: Inner1 { x: 15 },
        };
        let cloned: S2<String> = original.clone_into();
        assert_eq!(original, cloned);
        let double_cloned = S1::clone_from(&cloned);
        assert_eq!(original, double_cloned)
    }
}