Skip to content

Commit 2a8f33b

Browse files
committed
docs: Update readme
1 parent e7d2087 commit 2a8f33b

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

README.md

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@
55
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
66
[![Downloads](https://img.shields.io/github/downloads/nukesor/inter-struct/total.svg)](https://github.com/nukesor/inter-struct/releases)
77

8-
Various derive macros for trait implementations betwen two structs.
8+
A `#[derive()]` macro for various trait implementations between two structs.
99

1010
Please read the **known caveats** section before using this crate!
1111
It's not trivial to implement code for two structs in the same codebase.
1212

13-
### Planned features:
13+
Also note that this crate is in an early development phase.
14+
The crate is already properly tested, but bugs might still be there.
15+
16+
## Features:
1417

1518
- [ ] PartialEq
16-
- [x] StructMerge
17-
- [x] StructMergeRef
19+
- [x] Merge
20+
- [x] MergeRef
1821
- [x] Into - A standard `From/Into` impl between two structs.
1922
- [x] IntoDefault - `From/Into`, but use `Default` on the target for unknown fields.
23+
- [ ] Field attributes to customize the behavior of our traits.
24+
* [ ] `unchecked` Ignore any type checks done by inter-struct and let the compiler handle errors.
25+
* [ ] `rename` Similar to serde's rename, map a field to another named field.
26+
* [ ] `ignore` Ignore this field in the type generation.
2027

21-
### StructMerge
28+
## Merge
2229

2330
```rust,ignore
2431
/// Merge another struct into Self whilst consuming it.
@@ -30,8 +37,7 @@ pub trait StructMerge<Src> {
3037
}
3138
```
3239

33-
34-
This following code is an example on how to use the `InterStruct` derive macro for implementing the `StructMerge` trait between to structs.
40+
This following code is an example on how to use the `InterStruct` derive macro for implementing the `StructMerge` trait between two structs.
3541

3642
```rust,ignore
3743
use inter_struct::prelude::*;
@@ -42,13 +48,13 @@ pub struct Target {
4248
pub optional: String,
4349
/// This field won't be touched as the macro cannot find a
4450
/// respective `ignored` field in the `Source` struct.
45-
pub ignored: Option<String>,
51+
pub ignored: String,
4652
}
4753
4854
/// A struct with both an identical and an optional field type.
4955
/// Note that the path to `Target` must always be fully qualifying.
5056
#[derive(InterStruct)]
51-
#[merge("crate::structs::Target")]
57+
#[merge("crate::Target")]
5258
pub struct Source {
5359
pub normal: String,
5460
pub optional: Option<String>,
@@ -79,21 +85,61 @@ fn main() {
7985
```
8086

8187

88+
## Into
8289

83-
## Known caveats
8490

85-
The main problems in this crate come from module resolution during.
86-
There's no official way to resolve modules or types in the the procedural macro stage, which is why this crate only supports specific cases.
91+
This following code is an example on how to use the `InterStruct` derive macro for implementing `Into` between two structs.
92+
93+
```rust,ignore
94+
use inter_struct::prelude::*;
95+
96+
/// The target struct we'll convert our `Source` struct into.
97+
pub struct Target {
98+
pub normal: String,
99+
pub optional: String,
100+
}
87101
88-
It's designed to work in this environment:
102+
#[derive(InterStruct)]
103+
// Note that the path to `Target` must always be fully qualifying.
104+
#[into("crate::Target")]
105+
pub struct Source {
106+
pub normal: String,
107+
pub optional: Option<String>,
108+
/// This field doesn't exist in the target, hence it'll be ignored.
109+
pub ignored: String,
110+
}
111+
112+
fn main() {
113+
let source = Source {
114+
/// Has the same type as Target::normal
115+
normal: "source".to_string(),
116+
/// Wraps Target::optional in an Option
117+
optional: Some("source".to_string()),
118+
ignored: "source".to_string(),
119+
};
120+
121+
// Merge the `Source` struct into target.
122+
let target: Target = source.into();
123+
assert_eq!(target.normal, "source".to_string());
124+
assert_eq!(target.optional, Some("source".to_string()));
125+
}
126+
```
127+
128+
## Known caveats
129+
130+
Inter-struct is designed to work in this environment:
89131

90132
- In the scope of a single crate. Cross-crate usage won't work.
91133
- In the main `src` folder of the crate. Integration tests and examples aren't supported.
92134

93-
Depending on your crate's structure, the module resolution might not work as expected.
94-
As we're creating safe and valid Rust code the compiler will thrown an error if any problems arise.
135+
The main problems in this crate come from the fact that there's no official way to resolve modules or types in the the procedural macro stage.
136+
137+
Due to this limitation, inter-struct isn't capable of ensuring the equality of two types.
138+
As a result, it might create false negative compile errors, even though the types might be compatible.
139+
This might happen if, for instance, types are obscured via an alias or if a type can be automatically dereferenced into another type.
140+
141+
However, as we're creating safe and valid Rust code, the compiler will thrown an error if any type problems arise.
95142

96-
Limitations in type resolution might also lead to wrong code generation for types that are obscured by type aliases, but the compiler should still throw errors in those cases.
97143

98144
#### Not yet solved problems
99145

@@ -113,9 +159,9 @@ These are problems that can probably be solved but they're non-trivial.
113159
#### Unsolvable problems
114160

115161
These are problems that are either impossible to solve or very infeasible.
116-
For instance, something infeasible would be to parse all files and to do a full type resolution of a given crate.
117-
That would be a job for the compiler in later stages.
162+
For instance, something infeasible would be to parse all files for a full type resolution of a given crate.
163+
That would be a job for the compiler in a later stage.
118164

119165
- Structs that are altered or generated by other macros.
120-
- Type aliases. E.g. `type test = Option<String>` won't be detected as an Option.
121-
The current check for `Option` fields is a literal check for the `Option` token.
166+
- Type comparison and type resolution. E.g. `type test = Option<String>` won't be detected as optional.
167+
The current type checks are literal comparisons of the type tokens.

testing/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name = "testing"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
76
[dependencies]
87
#inter-struct = { path = "..", features = ["debug"] }
98
inter-struct = { path = ".." }

0 commit comments

Comments
 (0)