Debian Codegraph @

debian/rust-vendor/self_cell/README.md

raw
1 [<img alt="github" src="https://img.shields.io/badge/github-self__cell-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/Voultapher/self_cell)
2 [<img alt="crates.io" src="https://img.shields.io/badge/dynamic/json?color=fc8d62&label=crates.io&query=%24.crate.max_version&url=https%3A%2F%2Fcrates.io%2Fapi%2Fv1%2Fcrates%2Fself_cell&style=for-the-badge&logo=rust" height="20">](https://crates.io/crates/self_cell)
3 [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-self__cell-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/self_cell)
4
5 # `self_cell!`
6
7 Use the macro-rules macro: `self_cell!` to create safe-to-use self-referential
8 structs in stable Rust, without leaking the struct internal lifetime.
9
10 In a nutshell, the API looks *roughly* like this:
11
12 ```rust
13 // User code:
14
15 self_cell!(
16 struct NewStructName {
17 owner: Owner,
18
19 #[covariant]
20 dependent: Dependent,
21 }
22
23 impl {Debug}
24 );
25
26 // Generated by macro:
27
28 struct NewStructName(...);
29
30 impl NewStructName {
31 fn new(
32 owner: Owner,
33 dependent_builder: impl for<'a> FnOnce(&'a Owner) -> Dependent<'a>
34 ) -> NewStructName { ... }
35 fn borrow_owner<'a>(&'a self) -> &'a Owner { ... }
36 fn borrow_dependent<'a>(&'a self) -> &'a Dependent<'a> { ... }
37 [...]
38 // See the macro level documentation for a list of all generated functions
39 // and other possible options, e.g. async builder support,
40 // https://docs.rs/self_cell/latest/self_cell/macro.self_cell.html#generated-api.
41 }
42
43 impl Debug for NewStructName { ... }
44 ```
45
46 Self-referential structs are currently not supported with safe vanilla Rust. The
47 only reasonable safe alternative is to expect the user to juggle 2 separate data
48 structures which is a mess. The library solution ouroboros is expensive to
49 compile due to its use of procedural macros.
50
51 This alternative is `no_std`, uses no proc-macros, some self contained unsafe
52 and works on stable Rust, and is miri tested. With a total of less than 300
53 lines of implementation code, which consists mostly of type and trait
54 implementations, this crate aims to be a good minimal solution to the problem of
55 self-referential structs.
56
57 It has undergone [community code review](https://users.rust-lang.org/t/experimental-safe-to-use-proc-macro-free-self-referential-structs-in-stable-rust/52775)
58 from experienced Rust users.
59
60 ### Fast compile times
61
62 ```
63 $ rm -rf target && cargo +nightly build -Z timings
64
65 Compiling self_cell v0.9.0
66 Completed self_cell v0.9.0 in 0.2s
67 ```
68
69 Because it does **not** use proc-macros, and has 0 dependencies compile-times
70 are fast.
71
72 Measurements done on a slow laptop.
73
74 ### A motivating use case
75
76 ```rust
77 use self_cell::self_cell;
78
79 #[derive(Debug, Eq, PartialEq)]
80 struct Ast<'a>(pub Vec<&'a str>);
81
82 self_cell!(
83 struct AstCell {
84 owner: String,
85
86 #[covariant]
87 dependent: Ast,
88 }
89
90 impl {Debug, Eq, PartialEq}
91 );
92
93 fn build_ast_cell(code: &str) -> AstCell {
94 // Create owning String on stack.
95 let pre_processed_code = code.trim().to_string();
96
97 // Move String into AstCell, then build Ast inplace.
98 AstCell::new(
99 pre_processed_code,
100 |code| Ast(code.split(' ').filter(|word| word.len() > 1).collect())
101 )
102 }
103
104 fn main() {
105 let ast_cell = build_ast_cell("fox = cat + dog");
106
107 println!("ast_cell -> {:?}", &ast_cell);
108 println!("ast_cell.borrow_owner() -> {:?}", ast_cell.borrow_owner());
109 println!("ast_cell.borrow_dependent().0[1] -> {:?}", ast_cell.borrow_dependent().0[1]);
110 }
111 ```
112
113 ```
114 $ cargo run
115
116 ast_cell -> AstCell { owner: "fox = cat + dog", dependent: Ast(["fox", "cat", "dog"]) }
117 ast_cell.borrow_owner() -> "fox = cat + dog"
118 ast_cell.borrow_dependent().0[1] -> "cat"
119 ```
120
121 There is no way in safe Rust to have an API like `build_ast_cell`, as soon as
122 `Ast` depends on stack variables like `pre_processed_code` you can't return the
123 value out of the function anymore. You could move the pre-processing into the
124 caller but that gets ugly quickly because you can't encapsulate things anymore.
125 Note this is a somewhat niche use case, self-referential structs should only be
126 used when there is no good alternative.
127
128 Under the hood, it heap allocates a struct which it initializes first by moving
129 the owner value to it and then using the reference to this now Pin/Immovable
130 owner to construct the dependent inplace next to it. This makes it safe to move
131 the generated SelfCell but you have to pay for the heap allocation.
132
133 See the documentation for a more in-depth API overview and advanced examples:
134 https://docs.rs/self_cell
135
136 ### Installing
137
138 [See cargo docs](https://doc.rust-lang.org/cargo/guide/).
139
140 ## Running the tests
141
142 ```
143 cargo test
144
145 cargo miri test
146 ```
147
148 ### Related projects
149
150 - [ouroboros](https://github.com/joshua-maros/ouroboros)
151 - [rental](https://github.com/jpernst/rental) | soundness issues (tests fail with recent miri versions) and [deprecated](https://github.com/jpernst/rental#warning-this-crate-is-no-longer-maintained-or-supported)
152 - [Schroedinger](https://github.com/dureuill/sc) | [soundness issues](https://github.com/dureuill/sc/issues/1)
153 - [owning_ref](https://github.com/Kimundi/owning-ref-rs) | [soundness issues](https://rustsec.org/advisories/RUSTSEC-2022-0040.html) and [seems unmaintained](https://github.com/Kimundi/owning-ref-rs/issues/81)
154 - [ghost-cell](https://github.com/matthieu-m/ghost-cell)
155 - [qcell](https://github.com/uazu/qcell/)
156 - [selfref](https://docs.rs/selfref)
157
158 ## Min required rustc version
159
160 By default the minimum required rustc version is 1.51.
161
162 There is an optional feature you can enable called "old_rust" that enables
163 support down to rustc version 1.36. However this requires polyfilling std
164 library functionality for older rustc with technically UB versions. Testing does
165 not show older rustc versions (ab)using this. Use at your own risk.
166
167 Using the `async_builder` option requires Rust 1.85 or newer.
168
169 The minimum versions are best-effort and may change with any new major release.
170
171 ## Contributing
172
173 Please respect the [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) when contributing.
174
175 ## Versioning
176
177 We use [SemVer](http://semver.org/) for versioning. For the versions available,
178 see the [tags on this repository](https://github.com/Voultapher/self_cell/tags).
179
180 ## Authors
181
182 * **Lukas Bergdoll** - *Initial work* - [Voultapher](https://github.com/Voultapher)
183
184 See also the list of [contributors](https://github.com/Voultapher/self_cell/contributors)
185 who participated in this project.
186
187 ## License
188
189 This project is dual licensed under the Apache License Version 2.0 OR GNU General Public License Version 2.0 -
190 see the [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-GPLv2](LICENSE-GPLv2) for details.