| 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 |
|
| 39 |
impl Debug for NewStructName { ... } |
| 40 |
``` |
| 41 |
|
| 42 |
Self-referential structs are currently not supported with safe vanilla Rust. The |
| 43 |
only reasonable safe alternative is to expect the user to juggle 2 separate data |
| 44 |
structures which is a mess. The library solution ouroboros is really expensive |
| 45 |
to compile due to its use of procedural macros. |
| 46 |
|
| 47 |
This alternative is `no_std`, uses no proc-macros, some self contained unsafe |
| 48 |
and works on stable Rust, and is miri tested. With a total of less than 300 |
| 49 |
lines of implementation code, which consists mostly of type and trait |
| 50 |
implementations, this crate aims to be a good minimal solution to the problem of |
| 51 |
self-referential structs. |
| 52 |
|
| 53 |
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) |
| 54 |
from experienced Rust users. |
| 55 |
|
| 56 |
### Fast compile times |
| 57 |
|
| 58 |
``` |
| 59 |
$ rm -rf target && cargo +nightly build -Z timings |
| 60 |
|
| 61 |
Compiling self_cell v0.9.0 |
| 62 |
Completed self_cell v0.9.0 in 0.2s |
| 63 |
``` |
| 64 |
|
| 65 |
Because it does **not** use proc-macros, and has 0 dependencies compile-times |
| 66 |
are fast. |
| 67 |
|
| 68 |
Measurements done on a slow laptop. |
| 69 |
|
| 70 |
### A motivating use case |
| 71 |
|
| 72 |
```rust |
| 73 |
use self_cell::self_cell; |
| 74 |
|
| 75 |
#[derive(Debug, Eq, PartialEq)] |
| 76 |
struct Ast<'a>(pub Vec<&'a str>); |
| 77 |
|
| 78 |
self_cell!( |
| 79 |
struct AstCell { |
| 80 |
owner: String, |
| 81 |
|
| 82 |
#[covariant] |
| 83 |
dependent: Ast, |
| 84 |
} |
| 85 |
|
| 86 |
impl {Debug, Eq, PartialEq} |
| 87 |
); |
| 88 |
|
| 89 |
fn build_ast_cell(code: &str) -> AstCell { |
| 90 |
// Create owning String on stack. |
| 91 |
let pre_processed_code = code.trim().to_string(); |
| 92 |
|
| 93 |
// Move String into AstCell, then build Ast inplace. |
| 94 |
AstCell::new( |
| 95 |
pre_processed_code, |
| 96 |
|code| Ast(code.split(' ').filter(|word| word.len() > 1).collect()) |
| 97 |
) |
| 98 |
} |
| 99 |
|
| 100 |
fn main() { |
| 101 |
let ast_cell = build_ast_cell("fox = cat + dog"); |
| 102 |
|
| 103 |
println!("ast_cell -> {:?}", &ast_cell); |
| 104 |
println!("ast_cell.borrow_owner() -> {:?}", ast_cell.borrow_owner()); |
| 105 |
println!("ast_cell.borrow_dependent().0[1] -> {:?}", ast_cell.borrow_dependent().0[1]); |
| 106 |
} |
| 107 |
``` |
| 108 |
|
| 109 |
``` |
| 110 |
$ cargo run |
| 111 |
|
| 112 |
ast_cell -> AstCell { owner: "fox = cat + dog", dependent: Ast(["fox", "cat", "dog"]) } |
| 113 |
ast_cell.borrow_owner() -> "fox = cat + dog" |
| 114 |
ast_cell.borrow_dependent().0[1] -> "cat" |
| 115 |
``` |
| 116 |
|
| 117 |
There is no way in safe Rust to have an API like `build_ast_cell`, as soon as |
| 118 |
`Ast` depends on stack variables like `pre_processed_code` you can't return the |
| 119 |
value out of the function anymore. You could move the pre-processing into the |
| 120 |
caller but that gets ugly quickly because you can't encapsulate things anymore. |
| 121 |
Note this is a somewhat niche use case, self-referential structs should only be |
| 122 |
used when there is no good alternative. |
| 123 |
|
| 124 |
Under the hood, it heap allocates a struct which it initializes first by moving |
| 125 |
the owner value to it and then using the reference to this now Pin/Immovable |
| 126 |
owner to construct the dependent inplace next to it. This makes it safe to move |
| 127 |
the generated SelfCell but you have to pay for the heap allocation. |
| 128 |
|
| 129 |
See the documentation for a more in-depth API overview and advanced examples: |
| 130 |
https://docs.rs/self_cell |
| 131 |
|
| 132 |
### Installing |
| 133 |
|
| 134 |
[See cargo docs](https://doc.rust-lang.org/cargo/guide/). |
| 135 |
|
| 136 |
## Running the tests |
| 137 |
|
| 138 |
``` |
| 139 |
cargo test |
| 140 |
|
| 141 |
cargo miri test |
| 142 |
``` |
| 143 |
|
| 144 |
### Related projects |
| 145 |
|
| 146 |
[ouroboros](https://github.com/joshua-maros/ouroboros) |
| 147 |
|
| 148 |
[rental](https://github.com/jpernst/rental) |
| 149 |
|
| 150 |
[Schroedinger](https://github.com/dureuill/sc) |
| 151 |
|
| 152 |
[owning_ref](https://github.com/Kimundi/owning-ref-rs) |
| 153 |
|
| 154 |
[ghost-cell](https://github.com/matthieu-m/ghost-cell) |
| 155 |
|
| 156 |
## Min required rustc version |
| 157 |
|
| 158 |
By default the minimum required rustc version is 1.51. |
| 159 |
|
| 160 |
There is an optional feature you can enable called "old_rust" that enables |
| 161 |
support down to rustc version 1.36. However this requires polyfilling std |
| 162 |
library functionality for older rustc with technically UB versions. Testing does |
| 163 |
not show older rustc versions (ab)using this. Use at your own risk. |
| 164 |
|
| 165 |
The minimum versions are a best effor and may change with any new major release. |
| 166 |
|
| 167 |
## Contributing |
| 168 |
|
| 169 |
Please respect the [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) when contributing. |
| 170 |
|
| 171 |
## Versioning |
| 172 |
|
| 173 |
We use [SemVer](http://semver.org/) for versioning. For the versions available, |
| 174 |
see the [tags on this repository](https://github.com/Voultapher/self_cell/tags). |
| 175 |
|
| 176 |
## Authors |
| 177 |
|
| 178 |
* **Lukas Bergdoll** - *Initial work* - [Voultapher](https://github.com/Voultapher) |
| 179 |
|
| 180 |
See also the list of [contributors](https://github.com/Voultapher/self_cell/contributors) |
| 181 |
who participated in this project. |
| 182 |
|
| 183 |
## License |
| 184 |
|
| 185 |
This project is licensed under the Apache License, Version 2.0 - |
| 186 |
see the [LICENSE.md](LICENSE.md) file for details. |