├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md └── src ├── lib.rs └── list_of_the_damned.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "sinner" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sinner" 3 | version = "0.1.1" 4 | edition = "2021" 5 | license = "MIT" 6 | description = "Easy cross-thread resource sharing for Rust!" 7 | repository = "https://github.com/eugeny/rust-sinner/" 8 | 9 | [lib] 10 | path = "src/lib.rs" 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust S̵̓i̸̓n̵̉ 2 | 3 | [![Crates.io](https://img.shields.io/static/v1?color=green&label=crates.io&message=666)](https://crates.io/crates/sinner) 4 | 5 | _I̴n̴f̶e̸r̵n̷a̴l mutability!_ 6 | 7 | --- 8 | 9 | Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when you need it? Your thread function requires move semantics, but you j̸̉us†̸ ain't got time for †̸̰͋h̸ą̷̊͝t? 10 | 11 | W̵e̴l̵l̸ ̵w̶o̴r̶r̴y̶ ̵n̶o̶ ̷m̴o̷r̶e̴,̸, just sprinkle some _s̴̖̑í̵̲ṋ̵̀_ on it and call it a day! 12 | 13 | ``` 14 | sinner = "0.1" 15 | ``` 16 | 17 | ## Examples 18 | 19 | ### Threads 20 | 21 | Threads and ȋ̷̖n̸̨̈́f̷̆ͅe̷͑ͅr̵̝͆ï̴̪o̸̡̔r̷͉͌ m̶u̸t̷a̵b̶i̵l̸i̷t̵y̶? No problem amigo! 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 61 | 93 | 94 | 95 | 118 | 128 | 129 |
Before (YIKES!):After (GLORIOUS!):
30 | 31 | ```rust 32 | 33 | use std::thread::{spawn, sleep}; 34 | 35 | 36 | struct Stuff { 37 | pub content: String, 38 | } 39 | 40 | impl Stuff { 41 | fn bang(&self) { 42 | println!("bang! {}", self.content); 43 | } 44 | } 45 | 46 | fn main() { 47 | let mut x = Stuff { content: "old".to_string() }; 48 | let t = spawn({ 49 | move || { 50 | sleep(std::time::Duration::from_secs(1)); 51 | x.content = "new".to_string(); 52 | x.bang(); 53 | } 54 | }); 55 | x.bang(); 56 | t.join().unwrap(); 57 | x.bang(); 58 | } 59 | ``` 60 | 62 | 63 | ```rust 64 | use sinner::Sin; // <-- 65 | use std::thread::{spawn, sleep}; 66 | 67 | #[derive(Debug, Clone)] 68 | struct Stuff { 69 | pub content: String, 70 | } 71 | 72 | impl Stuff { 73 | fn bang(&self) { 74 | println!("bang! {}", self.content); 75 | } 76 | } 77 | 78 | fn main() { 79 | let mut x = Sin::new(Stuff { content: "old".to_string() }); // <-- 80 | let t = spawn({ 81 | move || { 82 | sleep(std::time::Duration::from_secs(1)); 83 | x.content = "new".to_string(); 84 | x.bang(); 85 | } 86 | }); 87 | x.bang(); 88 | t.join().unwrap(); 89 | x.bang(); 90 | } 91 | ``` 92 |
96 | 97 | ``` 98 | error[E0382]: borrow of moved value: `x` 99 | --> src/main.rs:144:5 100 | | 101 | 136 | let mut x = Stuff { content: "old".to_string() }; 102 | | ----- move occurs because `x` has type `Stuff` 103 | 137 | let t = spawn({ 104 | 138 | move || { 105 | | ------- value moved into closure here 106 | ... 107 | 141 | x.bang(); 108 | | - variable moved due to use in closure 109 | ... 110 | 144 | x.bang(); 111 | | ^^^^^^^^ value borrowed here after move 112 | 113 | For more information about this error, try `rustc --explain E0382`. 114 | error: could not compile `sinner` due to previous error 115 | ``` 116 | 117 | 119 | 120 | ``` 121 | Finished dev [unoptimized + debuginfo] target(s) in 0.37s 122 | Running `target/debug/sinner` 123 | bang! old 124 | bang! n̴e̷w̸ 125 | b̸̙̚a̸̘̓n̵̥̔g̵͚̓!̵̤̓ ǹ̴̘e̵̺̾w̴̛̦ 126 | ``` 127 |
130 | 131 | ### Doubly-linked list 132 | 133 | With just a̸ ̸b̵i̸t̴ of _sin_, you can do doubly-linked lists in no time! 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 200 | 260 | 261 | 262 | 297 | 308 | 309 | 310 |
Before (UGH!):After (JOLLY!):
142 | 143 | ```rust 144 | 145 | 146 | 147 | struct Item { 148 | pub prev: Option, 149 | pub next: Option, 150 | pub value: T 151 | } 152 | 153 | struct List { 154 | pub head: Option>, 155 | pub tail: Option>, 156 | } 157 | 158 | impl Default for List { 159 | fn default() -> Self { 160 | List { 161 | head: None, 162 | tail: None, 163 | } 164 | } 165 | } 166 | 167 | impl List where T: Clone { 168 | pub fn append(&mut self, other: T) { 169 | let mut item = Item { 170 | prev: None, 171 | next: None, 172 | value: other, 173 | }; 174 | if let Some(ref mut tail) = self.tail { 175 | tail.next = Some(item); 176 | item.prev = Some(tail.clone()); 177 | self.tail = Some(item); 178 | } else { 179 | self.head = Some(item); 180 | self.tail = Some(item); 181 | } 182 | } 183 | } 184 | 185 | fn main () { 186 | let mut list = List::default(); 187 | list.append(1); 188 | list.append(2); 189 | list.append(3); 190 | 191 | let mut ptr = list.head; 192 | while let Some(item) = ptr { 193 | println!("{}", item.value); 194 | ptr = item.next; 195 | } 196 | } 197 | ``` 198 | 199 | 201 | 202 | ```rust 203 | use sinner::Sin; // <-- 204 | 205 | #[derive(Clone)] 206 | struct Item { 207 | pub prev: Option>, // <-- 208 | pub next: O̷p̷t̵i̴o̷n̵≮S̶i̴n̶>, // <-- 209 | pub value: T 210 | } 211 | 212 | struct List { 213 | pub head: Option>>, // <-- 214 | pub tail: Option>>, // <-- 215 | } 216 | 217 | impl Default for List { 218 | fn default() -> Self { 219 | List { 220 | head: None, 221 | tail: None, 222 | } 223 | } 224 | } 225 | 226 | impl List where T: Clone { 227 | pub fn append(&mut self, other: T) { 228 | let mut item = S̸i̷n̴:̵:̷n̸e̸w̷(̶I̵t̵e̴m̷ { // <-- 229 | p̷r̴e̴v̶:̴ ̶N̸o̴n̷e̷,̴ 230 | n̵e̴x̸t̶:̷ ̸N̸o̴n̸e̸,̶ 231 | v̵a̷l̸u̶e̴:̷ ̴o̸t̸h̸e̷r̶, 232 | }); 233 | if let Some(ref mut tail) = self.tail { 234 | tail.next = Some(item); 235 | item.prev = Some(tail.clone()); 236 | self.tail = Some(item); 237 | } else { 238 | self.head = Some(item); 239 | self.tail = Some(item); 240 | } 241 | } 242 | } 243 | 244 | fn main () { 245 | let mut list = List::default(); 246 | list.append(1); 247 | list.append(2); 248 | list.append(3); 249 | 250 | let mut ptr = list.head; 251 | while let Some(item) = ptr { 252 | println!("{}", item.value); 253 | ptr = item.next; 254 | } 255 | } 256 | ``` 257 | 258 | 259 |
263 | 264 | 265 | ``` 266 | error[E0072]: recursive type `Item` has infinite size 267 | --> src/main.rs:71:1 268 | | 269 | 71 | struct Item { 270 | | ^^^^^^^^^^^^^^ recursive type has infinite size 271 | 72 | pub prev: Option, 272 | | ------------ recursive without indirection 273 | 73 | pub next: Option, 274 | | ------------ recursive without indirection 275 | | 276 | help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `Item` representable 277 | | 278 | 72 ~ pub prev: Box>, 279 | 73 ~ pub next: Box>, 280 | | 281 | 282 | --> src/main.rs:100:35 283 | | 284 | 100 | item.prev = Some(tail.clone()); 285 | | ^^^^^ method not found in `&mut Item` 286 | | 287 | = help: items from traits can only be used if the trait is implemented and in scope 288 | = note: the following trait defines an item `clone`, perhaps you need to implement it: 289 | candidate #1: `Clone` 290 | 291 | Some errors have detailed explanations: E0072, E0599. 292 | For more information about an error, try `rustc --explain E0072`. 293 | error: could not compile `sinner` due to 2 previous errors 294 | ``` 295 | 296 | 298 | 299 | ``` 300 | Finished dev [unoptimized + debuginfo] target(s) in 0.00s 301 | R̸u̷n̴n̴i̷n̸g̸ ̸`̶t̸a̸r̷g̵e̶t̶/̸d̴e̵b̴u̷g̶/ç̴̠͌̚u̵̦̅r̶̓̆͜͜s̶̤̫̊̕e̴͇̼̔ḑ̸̇ ̷̩̜̓c̶̯͆ḧ̶̯́͝ì̶̗̣̆l̸̪̓̈́d̵̪̔̀`̷ 302 | ̴1̴ 303 | ̸2̷ 304 | ̶3̶ 305 | ``` 306 | 307 |
311 | 312 | 313 | ## Frequently asked q̴u̷estions 314 | 315 | ### Can a̷̝͂n̸̝̈́y̷͈͋ ̷͓͐t̴̯̆ŷ̶̡p̵̯͆é̴̙ ̷̰̃ḇ̷̈e̴̥̕ š̶̢̯͒̉ȃ̶̞͚̩͊̈́̌c̵̰͍̍͛r̸̨̛̀̽̏͂ḯ̴͈̘̇͛f̶̪̬̼̌̾ì̵̩c̵̱̹̖͒́͑͗̔e̸̤̜͒̈̐d̵͚̞̗̠̽͘ 316 | 317 | Well, why, almost! The type parameter needs to implement `Clone` due internal library s̵t̴r̸u̶c̵t̴u̸r̴e̵s̵. 318 | 319 | ### Is access to the underlying value t̷h̸r̶e̶a̷d̵-s̷̵̸̙̖͊̈ǻ̷̵̸̫̜f̷̴̸̖͙̂̍e̶̸̷̪͚̽̐??̷̰̰̊̅͜ 320 | 321 | I̷ ̵c̴a̷n̷ ̷a̶s̶s̵u̶r̴e̸ ̷y̶o̶u̴ t̸h̸a̴t̸ ̷t̴h̶e̶r̷e̷ ̸i̷s̶ n̴͔̱̮̅o̷̢̦̔͒̓ ̶͙̱̦͗̅͐n̸͉͌̓é̵̥̲è̷͉̳͓̽d̴̜̻̱̀̑ t̷͉͊o̸͎̅ ̵̟͗ç̴̚ỏ̶̮n̸͌ͅc̴̩͐ȩ̸̉ȑ̴̹ǹ̸͜ ̷͖̎y̷̞̽o̵͚͐u̶̜̎r̷̹͑ś̸͙e̶̜͛l̵͚̽f̴͕̍ ẘ̵̮͂̕ĩ̷̖͇͚̗ţ̶͈̥̝̭͐͑̀̃͠h̴̨̰͖̥̼͂͑ ̵͔̞̀͐̌̚͝s̴̢̥͔͇̽͂̏͜ǔ̵̢̥͍̼̂c̴̘̜͒̂̽̃h̵̨̞̗͍͕́̊͌̎̒ ̸̨̟̫͊ͅQ̸̹̠͙͎̉U̴̞̫͉̬̜̅̂͂Ẹ̷̛̰̼̋̎́̚S̸̗͇̞̜̎̆̏̀T̸͎̙̰̠̦̈́͘I̶̧̘̯͙͋̏̂͘̚ͅO̶̼͍͘͝N̶̪͇̑S̷͂̌̍͆ͅ Y̴̥̋̑̆͋͝O̶̡̗̣̤͕̰͒U̸̢͙͇̠̫͊͒̋͋̕͠ ̷̮̳̮̭̔̿̈́́̎̕͝Ṇ̴̙͑͐̋̄̊Å̵̛̞̮͓̗̊́̽̈́̈͜Í̶̼̠͚̒́͑̃V̶̨̯̦̊͒̏͝Ē̴̡̥͕͙̙͔̕ ̵̨͕̜̮̞͖̳̀C̷̮̞̩͆̽̂͊͆͗ͅH̶̫̱̠̟̞͊̐I̸̞̰̩͎̫͐̈̆L̴̤̩̭̾D̷̹̿̍̏̂̓,̴̛͓̻͓̖̲͛͗͐̉̈́̀ ̴͈͍̳̥͘O̸̯͌̇́B̴̪̤̺̊E̴̞̩̲͂̇̕Ý̵͙̟̱̽̍̐͋̃. T̴̡̙̜̠̱̠͉̘̙̥̔̆̋̌̄͑̄̔H̶̯̦̥̙̩͑̍̓̑͗̄͛͛̕E̵̜̞̠̣̲͔̝̺͇͑̽̌͆ͅ ̵̧̖̞̣͇̻̾̉͛̈́̆̌͝Ẇ̷̡͚̺̇̈̄͐͊̓̽͘͜͝ͅĨ̵̭̪̲̖̕͘L̸̪͎͎̪͠ͅL̶̘̿͑͆ ̵̙̺̯̺͈̪̳̄̇͌̽̀̉͗͌̆Ȍ̴͖͐̇͑͗͗̾͝F̵̥̂̓͒̕ ̸̢̺̺͍̼͚̺̹̈́͋̀͊̆̌͠O̶̠͙̟̠̦̯͔̱̍͊͊̓̌͐̔͌̉͘Ù̴̝̞̥̰̙̰͂͒̽̎͂̕ͅR̶͖̫͍̫̹̜̤͎̮͉̈́͑ ̶͎̼̰̉̈̕͠ͅM̴̘͝A̴̼̋̓͐̇̀̉́͋̕S̶̭̹͕̲̬̪̫̖̮͍͌̌͐͝Ţ̶͍̠͓̞̙͔̞̲̣̓E̴͔̺͌͌̏̏̕Ř̷̡̬̞̣̠̬̪̝̉͗́,̶̩̪̮̺̉ 322 | 323 | 324 | 325 | 326 |

327 | 328 | Ŗ̶͖̟̫̝̣̗̤̤̥̰̯̭͕̺͙̮͙̙̲̄̃͒̅̓͗͛̂̑̎̏͐́́͊̐͐̕͘͘͘͝ͅͅȨ̶͓̭͔̦̣̲̘̹̫̬̥̻̗̗̖̻̜͖̟͖̀̉͋̽͒͒͑̊̀͋͋̎́͑̌̂̌̈͑́́̓̃̓̕͝͝͠J̶̢̛̮̪̪̣͎̫̫̮̞̲̦͖̹̦͖̱̙̯̗̲̀͊̏̎͑͂͑̓̇͌̿̎̔͒͊̍͑̇̌̎͆͋̕͠ͅͅÈ̷̢̧̨͔̩͕̬̩͖̱̙͔̞̲͎̮̙̲̝̮̟̦̲̤̝̌̀͋̒̈́̈́̄͊̓̇̃̍̿̃̒́̒̓̚̚͜͝ͅÇ̵̢̧̢̺̯͓̼̦̩̞̳̣̘̮̩̮̙̙͕̦̓̈͛͌̈̔́̐̇͆͐̈́̀̈́̀̐͂̓̏͒̈́́͘͜͝†̷̨̧̨̧̤͕̪͉̲̰͎̣͈̲̙͕̖̺̥̜̃̄̈́̃̾̌̄͑̿̒͒̊̍͛̅̏̉̄́̄͘̚̚̕͜͠͝͠ͅ ̵̛̰̪̮̫̪̲̬͉͇͔̤̥̬̳̥̝͚͇͖̘̤̟̀̓͗̈́͒͆͗̅̿̆͐̎̅̅̈͂̑͗͊̋̄̚̚͝͝M̴̢̧̘̮͉̺̻̼̮̬̮͕͎͉̰̩̪͎͓̣̹͍̌̓̃͛̀̊͑͑̓̈̌̅́̆͆̓̌͒̃͘̕Ę̷̥̜̞̰̭̞̟̦̺̦̝̱̙̠͕̬̩̮̞̬̙̳͓̀̓̃͂͛͊̓͌̃̅̇̋̇̇́̃̾͋̚͝͝͝͠͠͝ͅM̸̢̢̢̧̡̢̪͓̪̻̤̘̪̩̻͇̺̯̝̫̄̂̎̊̽̔̋́͑̇̑́͌̐̂͑̃̐̄̍̆̅ͅǪ̸̰̭̦͙̹̪͔̙̖͉̞͈̞̹̘̫̞̬͚̱̗̯̈́̇̓̔̍̓̔́̈́̽͑̑͑͒̏͂̕͘͘͝͠R̵̡̡̧̢̢̨̛̦̝͚̪̰̥̮̱̯̯̞͓̤̙͓͈̻̗̳̺͗́̌͛̅̔̇̑̆̀̽̅̽̋̏͘̕̚͝͝¥̵̧̛̛͙͖̣̪͚̠̪̹̫̰̲̗̥̘͖̥͕̮̫̳̱̯͂̅͛͑̌͌̎̏̈́̐̇̈́̅̽̀̉͊̾̀̉́͌̚͝͝ ̶̨̨̡̧̡̛̳̺͙̜̥͚͉̭͓͖͉̘̰͈̯̜̝̜̿̈́͛̊̀͆̅͌̑̏͗̏̎̈́͗̍̽̽̂̒̂̒̓͗̕͜͝ͅ§̸̢̧̛̺͈̹̺͕͔͍̠͖̗͕̦̟̣̜͖̠̫͖̞̎͗̐͒͆̈̌̍̀̂̏̆͊̄͗̊̾͘͠͠ͅÄ̷̧̡̩̪̟̟̟̻͓̙̻̜̦͈̗̻̤̱̦̎̀̄̈́̑̔͑̊͊͑͌̀̌̈̅̅͊̐̚͠ͅͅͅ£̷̧̢̢̡̡̛̬̼͔̬̹̘̪̯͕͎̰̟̮̖̠̯̰͛́̿͐̓́͐̎̅͑̍̿͗̿͒̈́̈͗̍͊̿̿̀̽̕͘͜ͅͅͅȨ̴̛̛͕͓͔̮͙̗̠͎̮̠̙͉̮̞̰̯̤̲͈̯̙̹̀̋̅͑̃͒̎̽͌͆̃̆̓͊͊̈́́̌̾̕͜͜͝͝͝ͅ†̴̧̡̺̯̠̳̳͎̻̱̟͓̥̠̱͙̦͖̝̣̞͓̗̲̔̾̈́̏̂̄̇̔̓̃̊̔͑͌̀̈́̇̒̏͝ͅ¥̶͎̬̟̻̬̞͈̞̱̪͔̦̩̥̭͓̬̰̩͖͍͉̓̈̊̍̀̇̈͊̾͋̽̂̅͑̂̓̓͌̊͜͜͝͠ 329 | 330 | 331 | 332 |

333 |

334 | 335 | 336 | # £̴̨̢̧̨̡̨̡̡̛̛̛͙̹̞͕̻͔̪̬̗̙͈̫̠͈̗͚̬͓͉͍͈̱͚̤͎̞̪̦͔͇̞̞͓͎̞͚̦̼̝̗̭̖͎̜̲̘̘͚͕̟̮̜̥̱͈̻̠͉̗̻̣̬̲̹̪̞̗͖̖̰̟̻͈̬͚̘͈̳̪̫͇̼͇͗͑͆̃͆̍̿́͊̒̄̍͛̆̄̿̇̀̿͊̌͒͋̌̃͂̃͛̉͑͂͊̂͂̀̈́̆͐̒̊̐̂̽̓̂͐́͂̏̈́̆̃͌̄̎̇͋͑̀̍̍̍͋́̾̍̍̇͛͌̈͆́̍͐̕͘̚͘̕͘͜͜͠͝͝͝ͅͅȨ̸̡̨̹̪͉̼̻͉̭͓̹̘͓͇̤̺̤̫̙͖̭̤͍̺̲̺̦̖̠͙̲̖̺̰͕̙͍̗͖͖͚̳̖̤̰̭͕̟̀̀͒͑̄̇͗̀̔́̋͊́͑̍͂̄̅̄͂̈́̏͌͗̽̒̌͐̎̕͜͝͝͝͠͝ͅͅÈ̵̡̢̧̡̢̢̢̛̛̛̝͕̠̖͓̗͈̲͇͓̦̜̭̞̪̻̠̩͙̪̝̝͕̪̼̪͚̟͖͓̰̼͔̩̰͖̳̳͕̝͉͖̰̥̰̖͇̤̝͇͙̙̘̤̀̋̓̍̏̽̋̌́̓̄͒͛̎͛͒̐͌͌͋̃͂̀̿̆̀̏̊̅̀̽̒͋̈̓̌̌̊̃̉̍͑͌̑̓̈͗́̊̂̊͒͐́̈́̇͂̉̂̊̀̿̓̃̈́̾́̓̒͂̔̋́̍̈́̑̋̿̾̋̅͛̈́̾̀͘̕̕̕͘͜͜͝͝͝͠ͅÐ̸̨̢̡̢̧̢̡̧̧̨̛̣̺̪̠̫͍̝̭̺̯̙̗͖͇̰͓̻̹̖̣͙̫̦̦̟͚͎̠̤̙̗̜͓͇͍̯͓̰̙̪̹̱͈̣̞̝̖̺̹̤̠̙̯̭͚͔̥̺̞̰͕͓̺̙͉͈̠͍̣̩͖̪̳̘͗̂̂͗̽̄͐̊͛̑̈́̍́̄͑͆̾͂̈́̏̀̾̓̋̔̊̌̔̅̂̀̍̌̿͐́̑̊̿͛͗̽̾̈͋̀͛̄̀͛͑̉̈̚̕͘͘͘̕͜͜͠͝͝͠͝ͅ ̸̧̢̡̧̧̧̛̛̛̛͕͈͔̜̣̦͇̥̦̟̖͈̣͙̖͓̤͔̮̙̳̰̜̫͙̞͈̭̖̥̟͓͇͙̠̼̳̠͎͙̱̹̱̦̯̮̩͉̺̺̪̤͔̻͇͓͍̟́̿̑̊̈́̔̒̅͋̾̓̉̈́̑̊͑̒̋̈́͂̅̓̀̓̾́̓̌͆̈́̀̓̃͌̍͂͒́̊͛̾̔̑́͑̃̓̓̒̉͐̈́͋̈́͗̄̆̂͆̌͆̈́̉̌̋̉̇̈́̌͌́͛͂͒͌̿͊̂͘͘͘̚̕͜͝͝͠͝͝ͅͅM̵̢̧̡̨̡̡̢̧̢̡̧̧̛̘̗̙̫̬̟̠͚̭̻͖̞̩͚̘̜̤̫̙̗͙̪͈̳̬̪͙̤̬̞̝̥̮͈̗̻͓̬̹̱̟̰͎̯̞̺͍͇̠̲̯͚̬̙̼͈͍̰̭̭̲͉͈̭͉̝͍̥̩̩̱̱̥͍̮̠͉̗̖̩͕̔̍̏̌̾̍̏̌̎̐̍̈̐͊̿͆͛̿̆̂͊͆͋̋̍́͊̎̓̀͒̆̀̈̆̈́̋̐́̎͘̚̚̚͘͠͠ͅͅĘ̶̧͚̼͉͎̰̝̺̳̝̠͚̗̺͖̫̗̫̭̩̝͖͓̠̰͉̦̻̰̹̱̖̭̣̹̘̩̀̅͂͒̊͋̔͑̀̏͐͂̈́̓̓̔́̆̀́̎͗͑͌̓͛̃̅̅͑̅̓̊͒̃͛͛̈͋̾̌̋͊̾͂͊̋͆͗̽̏̑͒̄̈̓̆͋̄͗̀͆̏̓̀̈́͐̇́͌͐̏̊̍͂́͂̅͂̈̏̿̆̑͐̽̾̉͛͐̚̚͘͠͝͝͠͝͝͠͠ͅͅ ̴̡̢̨̧̧̡̧̨̟͖͕̥̮̗͇̲̰̤̭̭̺̬͕͙̼̰̻̰̮͖̦̤̥̞̮̝̫̩̠̠͍̱͉̰̜̗͇̜̯̰͍͕̠̝͖͇̗͔̠̙̣̦̭͕̮̯͕͓͓͓̠̩͓̺͉̘̘̬͇̗̜̰̲̥̣͚̳͇̬͙̜̳̦̦͉̎̿̓̇̀̆̄̉̄͑̓͐̍̇̆́̈̄̽͊͆͆͒̆̈́̊̄̅̀͌̀͌̋̆̂̎̄̓̈́̾͌̎́̎́͑̈̊̅̅̾̍͗̔̂̌̔̎̓́͒̐̍̏͂͗́̌̄̊̀͋̓́̃̍̏̇̈́̄̉̆̀̋̚̕̕͘̚̕̚̚͜͠͝͠͝͝͝͝Ą̶̢̢̨̡̢̡̢̛̛̛̛͙̖͖̠̼̬͍̺̤̰̣͓̦̺͕̞̲͙̺̩̥̺̭͈̻̱̳̲͈̙͙͙̹̳̬̺͎̰̝̠̞̪̻̘̩̩͖̞͍̈͗͊̓̋̂̑̍̏͒̎̑͋̒̀͌̉͒̏̂̑̈́̒̅͆̄́̈͂̾̒̀͛̈́̊͋̈́̑̿̇̿͒́͊̍́̋̔͋̀͆̔̾̉̀̽̽͊́̌̍̄̀̇́͆̿̊͒̄̒̑͑́̕̚̚̕̚͜͜͜͠͝͠͠͠ͅͅͅ ̸̨̛͈̲̹̺̹̥͇͉̫͖̪̣͔̱̜̮͙̳̝̙͇̫͇̠͉̤̞̠̼̆̒̂̓̓̈́̉͋̎̔͐͆͑́͗̎̾̃̀̈́̀́͐̈̀̀́͒̾͛̀̐͑͑̽̂͐́̚̕͝͝͝͝͝§̵̡̧̨̨̨̡̡̡̢̡͉̪̣̣͚̹̣̜̣͉͚͕͖͚̼̻̤̠͖̙̳͇̹̞̦̭͕̲̝̼̝̼̭̝̮̣̳̬̘̼͍̝͇͓̠͙͓̟̬̤̹͓͕͉̠͓̝̻̌̍̿̂͆̎̇̂̋͌́̈́͋̇̈͛̂̓̒̌͐̋̉̅̋̾̌̃̇͊͌̄̄̇̇̆̽̊͑̅̑́͛̈̓̑̏̃̒͐̈́͑̚̚͘̕̕͘͝͝͝͝ͅͅ†̵̢̢̢̡̡̛̳̹̰̞̟͓̣̭̮̤̦͈͙̤͓̠̦̳̥̳͖̘̻̙̥͚̬̱͖̥̩̗͕̟̩͖̝̼̳̳̭͖̠̰̫̦̱̫̱̜̤̘̜̰͍̳̹̖̙̫̻̳̿̌̊͜͜͠R̸̨̧̢̧̢̧̡̢̧̬̰̥̦̘̠̱̲̩̝̙̼̜̦͉͔̝͚͔̮̱͍͇̠̱̭̞̘͙͎͖͔̟̪̻̗̠͇̲͙̙̮̪̖̯̟̝̠̙̋̎͐͌͛̈̎̓̂̐̂̀̃̓͒̀́̃̇̐̽͑̈̏̀̉̐̔̀̂̂͑͗̇̒̅̽̈̚̚͘͝͝͝͝ͅÄ̴̢͎̿̋̓̈́̐̅̚͘͜͠¥̷̧̢̢̧̛̛̛̛̛̣̺̜͎̘͓̝̳̝̞̺̫͓̖̥̖̲̣̪̦͖͕̬̱̹͕̤͚͎̹̮͚̮̟̞̗͚͈̪̼̘͇̗͙̻̩̱̜͇̮̖͇̝̞̣͔̠͌̀̓̓̓͆̋̊̊̋́̂̐̍̏̃͋̃̅̎̿̏̅̿̑́̈́̀͗̓͆́̎̓̄̈̅̒̎̃͛͋̎͐͐̒͂̑̌̈̈́̿̐͒̌͋̐̇̌̃̉͌̀̅͑́̀͂̄̈́̾̈́́͊̒̒͐̀͊͌̿̇̑̚͘̕̚̚͜͜͠͝͝͝͝ͅͅͅ ̶̧̢̢̢̨̛̛͚͚̟̰̠̲͎̥̻̰̘͎͔̳͚̥̻̬̻͍͕̥̝̱̤̰̖̭̭̗̹̥̦̟͈͚͔͎͇͚͓͉͕͙̰̩̳̪͕̮̜̖̰̟̦̹̳͚̦̩̯̰̣̈̿̅̋͛̈́̇̔̆̀̊͂͒͑̔̈̋̅͐̉̂̍͐̾̊̆̄̃͐̓̈́̽̎̇̉̈́̇̊̌̾͐̑̓̈́̀͆̒̍̄̈́̄͆̂̽̉̂̎̉͑̄͂̏́͒̄͆̐̔̽̈́̑͐͗͐͌̈̈́̈͊́͆̐͊͘͘̚̕̚͜͠͝͝͝͝͝͠ͅͅþ̸̨̢̧̡̨̧̧̨̛̦͓̫̤͍͎̻͔͉͓͚̯̠͓̤̯͕͈͍͎̯̱̳͇͖͔͕̜̞̠͙̲̳͖͙̖̺͔̗̖͖̥̩͇̱͚̲̟̪̖͙̙͙̥͔͉͖̥͉̯̝̗̜̗̤̇̊̈̂̎͒̊͌̉̊͊̅͊͑̎̌̌̿̀͑̋̇̒̚͘͘͜͜͝͠ͅͅǪ̸̢̡̧̢̢̧̧̧̧͓͔̙̘͖͉͈̗̜̲̺̲̪̮̠̰̭̜̯̙̰̫̟̩̹̫̱͖̲͚̠͖̣̝̞̠͇͎̝̺̟̰̠͇̙̞̯̞̤̭̬̞͈͈̯̰̙̠̈̽̈́͗̇̀̈́͊̊͑̈́̌́͊͛̀̀̋̕͜͜͜͝͝͝ͅͅͅÌ̷̡̡̢̢̨̛͖̠̥͔̠͎̘̫̤̗̤͍̩͔̲̜̹͙̼̯̭̺̖̩͖͈͓̫̦͉͈̜͉̤͕̪̦̩͇͓͔̥̣̯͍̙̗̞̱͍̟͕͔̹͖̜̬̘͇̹͚͕̗̻̥̤͉̠̣̤͎̝̥̰̑̿̓͐̇̇̃̊̈́̍̍̈͆̂͐̐̆̒͗̄̄̐͊͌̒͂̌̉̀̒̋͑̾̄̐̈́͆̆̀̔́̏̐̆̆̕̚̚͝͠͝͠ͅͅñ̴̠͙͍̯̱̞̫̩̘̱̞̝͖͎̲̻̣͚̺̟̳̥̭̳͙̞̥̗̭͕̰̫̗̀̈́́͒̀͊͗͐̋̀̍͆͌̀͒̈́̾̍̇̿͌͗̈̀̋̉̈́̇̆̊̊̄͑̓̐̾̈́̀̂̀̕͘̚͝ͅͅͅͅ†̸̢̛̛͈̙̤̭̩̱̺̬̻̺̖͍̙̣͇͌́̇́̉̍̇̃̒͋̌̀̒́͑̄͒̈́͌̾̐̎̉̇͒̔͂́͗̀̍́̓͐̒̅̐̉̔͆̄̾́̂͌̊̒̓͋̎͛̐̋̔̑̂̽̚̚͠͠͠͝͝͝͝Ę̸̨̛͇̗̬̭͔̭͕̳̜̫̜̟̝̪̯̲̩̠͕͙̝͓̤̩̝̪̘̘̪̥͔̠͔̘̳͇͓͖͓̗͈͙̣̭̳̬͇̱̱͖̯̦͙̳̞̰͈̟͓̼̯̣͉̖͈̀̎͐́̀͌͗̔̓͆̓̾͛̃̐̂͒̈̈́̀͌͐͛͑͛͋̽̓̔̈̊̽̔̒͐́̔͐̽͌͒̌̃̀͐͌͑̎̂̔̎̏̏́͋̇̑́͂̀̀̿̆̈͒̃̎̌̓͗̽̄́̀͊̏̑̂̔̃̃͑̃̃͂͊̌̏͌̀͘̕̕̚͘͜͜͝͝͝͝͝ͅR̷̨̡̨̛̤̻̺̲̥͉̬͍̳̤͙̩̱̯̲͚̰̣͍̺̝̟̣̼͈̥͕͇̦̙͇͚̤̺̰͇̽͌͂͌͛̐̿̎̈́̌̆̀̂̓͌͑̏̄̐̽̂̂̆͛͌̃̒͋̄̌͌̈́͌͋̉̈́͆͂̎̈̑̋̾̀̋̒͒͗̾́͌͐̃͒̌̚͜͠͠͠͝ 337 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::ops::{Deref, DerefMut}; 2 | use std::mem::MaybeUninit; 3 | 4 | #[cfg(not(debug_assertions))] 5 | compile_error!("D̴o̴n̸'̵t̷ ̷d̶e̵p̸l̶o̸y̵ ̵t̸h̷i̸s̸ ̵s̶h̴i̷t̷ ̵t̶o̴ ̷p̴r̸o̴d̶u̴c̷t̵i̵o̷n̴ ̷y̴o̷u̷ ̷m̶a̴d̸m̶a̸n̶"); 6 | 7 | mod list_of_the_damned; 8 | 9 | pub use list_of_the_damned::ListOfTheDamned; 10 | 11 | #[derive(Clone)] 12 | pub struct Sin { 13 | ptr: *mut Sinner, 14 | } 15 | 16 | pub struct Sinner { 17 | data: T, 18 | ref_count: usize, 19 | } 20 | 21 | unsafe impl Send for Sin {} 22 | impl Copy for Sin {} 23 | 24 | impl Sin { 25 | pub fn new(data: T) -> Self { 26 | let ptr = Box::into_raw(Box::new(Sinner { 27 | data, 28 | ref_count: 1, 29 | })); 30 | Sin { ptr: ptr } 31 | } 32 | 33 | fn clone(&self) -> Self { 34 | unsafe { 35 | (*self.ptr).ref_count += 1; 36 | } 37 | Sin { ptr: self.ptr } 38 | } 39 | } 40 | 41 | impl From for Sin { 42 | fn from(v: T) -> Self { 43 | Self::new(v) 44 | } 45 | } 46 | 47 | impl Deref for Sin { 48 | type Target = T; 49 | 50 | fn deref(&self) -> &T { 51 | unsafe { 52 | &(*self.ptr).data 53 | } 54 | } 55 | } 56 | 57 | impl DerefMut for Sin where T: Clone { 58 | fn deref_mut(&mut self) -> &mut T { 59 | unsafe { 60 | &mut (*self.ptr).data 61 | } 62 | } 63 | } 64 | 65 | pub trait UniversalSummoningCircle { 66 | fn summon() -> Self; 67 | } 68 | 69 | impl UniversalSummoningCircle for T { 70 | fn summon() -> Self { 71 | unsafe { 72 | MaybeUninit::uninit().assume_init() 73 | } 74 | } 75 | } 76 | 77 | #[test] 78 | fn summon_test() { 79 | let x = i32::summon(); 80 | let y = i32::summon(); 81 | let mut z = Vec::::summon(); 82 | z.push(x.to_string()); 83 | z.push(y.to_string()); 84 | } 85 | 86 | // impl Drop for Sin { 87 | // fn drop(&mut self) { 88 | // unsafe { 89 | // (*self.ptr).ref_count -= 1; 90 | // if (*self.ptr).ref_count == 0 { 91 | // drop(Box::from_raw(self.ptr)); 92 | // } 93 | // } 94 | // } 95 | // } 96 | 97 | 98 | // -- 99 | 100 | // struct Item { 101 | // pub prev: Option, 102 | // pub next: Option, 103 | // pub value: T 104 | // } 105 | 106 | // struct List { 107 | // pub head: Option>, 108 | // pub tail: Option>, 109 | // } 110 | 111 | // impl Default for List { 112 | // fn default() -> Self { 113 | // List { 114 | // head: None, 115 | // tail: None, 116 | // } 117 | // } 118 | // } 119 | 120 | // impl List where T: Clone { 121 | // pub fn append(&mut self, other: T) { 122 | // let mut item = Item { 123 | // prev: None, 124 | // next: None, 125 | // value: other, 126 | // }; 127 | // if let Some(ref mut tail) = self.tail { 128 | // tail.next = Some(item); 129 | // item.prev = Some(tail.clone()); 130 | // self.tail = Some(item); 131 | // } else { 132 | // self.head = Some(item); 133 | // self.tail = Some(item); 134 | // } 135 | // } 136 | // } 137 | 138 | // fn main () { 139 | // let mut list = List::default(); 140 | // list.append(1); 141 | // list.append(2); 142 | // list.append(3); 143 | 144 | // let mut ptr = list.head; 145 | // while let Some(item) = ptr { 146 | // println!("{}", item.value); 147 | // ptr = item.next; 148 | // } 149 | // } 150 | 151 | // use std::thread::{spawn, sleep}; 152 | 153 | // #[derive(Debug, Clone)] 154 | // struct Stuff { 155 | // pub content: String, 156 | // } 157 | 158 | // impl Stuff { 159 | // fn bang(&self) { 160 | // println!("bang! {}", self.content); 161 | // } 162 | // } 163 | 164 | // fn main() { 165 | // let mut x = Stuff { content: "old".to_string() }; 166 | // let t = spawn({ 167 | // move || { 168 | // sleep(std::time::Duration::from_secs(1)); 169 | // x.content = "new".to_string(); 170 | // x.bang(); 171 | // } 172 | // }); 173 | // x.bang(); 174 | // t.join().unwrap(); 175 | // x.bang(); 176 | // } 177 | -------------------------------------------------------------------------------- /src/list_of_the_damned.rs: -------------------------------------------------------------------------------- 1 | use std::marker::PhantomData; 2 | use std::ops::{Deref, DerefMut}; 3 | use std::{ptr, slice}; 4 | 5 | /// A list of infinite capacity with zero heap allocations. Assuming your machine doesn't succumb to 6 | /// demonic energy from merely thinking about it. 7 | #[repr(C)] 8 | pub struct ListOfTheDamned { 9 | len: usize, 10 | buf: PhantomData, 11 | } 12 | 13 | impl ListOfTheDamned { 14 | pub fn new() -> Self { 15 | Self { 16 | len: 0, 17 | buf: PhantomData, 18 | } 19 | } 20 | 21 | pub fn push(&mut self, item: T) { 22 | self.len += 1; 23 | unsafe { 24 | let buf = (&mut self.buf) as *mut PhantomData as *mut T; 25 | buf.offset(self.len as isize).write(item); 26 | } 27 | } 28 | 29 | pub fn insert(&mut self, index: usize, item: T) { 30 | self.len += 1; 31 | unsafe { 32 | let buf = (&mut self.buf) as *mut PhantomData as *mut T; 33 | if index + 1 != self.len { 34 | ptr::copy(buf.offset(index as isize), buf.offset((index + 1) as isize), self.len - index); 35 | } 36 | buf.offset(index as isize).write(item); 37 | } 38 | } 39 | 40 | pub fn remove(&mut self, index: usize) -> T { 41 | unsafe { 42 | let ret = ptr::read(&self.as_slice()[index]); 43 | if index + 1 != self.len { 44 | let buf = (&mut self.buf) as *mut PhantomData as *mut T; 45 | ptr::copy(buf.offset((index + 1) as isize), buf.offset(index as isize), self.len - index); 46 | } 47 | self.len -= 1; 48 | ret 49 | } 50 | } 51 | 52 | pub fn pop(&mut self) -> T { 53 | self.remove(self.len - 1) 54 | } 55 | 56 | pub fn clear(&self) { 57 | panic!("did you think you could be rid of us so easily?"); 58 | } 59 | 60 | pub fn is_empty(&self) -> bool { 61 | self.len() == 0 62 | } 63 | 64 | pub fn len(&self) -> usize { 65 | self.len 66 | } 67 | 68 | pub fn as_slice(&self) -> &[T] { 69 | &*self 70 | } 71 | 72 | pub fn as_mut_slice(&mut self) -> &mut [T] { 73 | &mut *self 74 | } 75 | 76 | pub fn swap_remove(&mut self, index: usize) -> T { 77 | let len = self.len; 78 | self.as_mut_slice().swap(len - 1, index); 79 | self.len -= 1; 80 | unsafe { 81 | ptr::read(&self.as_slice()[self.len]) 82 | } 83 | } 84 | } 85 | 86 | impl Deref for ListOfTheDamned { 87 | type Target = [T]; 88 | 89 | fn deref(& self) -> &Self::Target { 90 | unsafe { 91 | let buf = (&self.buf) as *const PhantomData as *const T; 92 | slice::from_raw_parts(buf, self.len) 93 | } 94 | } 95 | } 96 | 97 | impl DerefMut for ListOfTheDamned { 98 | fn deref_mut(&mut self) -> &mut Self::Target { 99 | unsafe { 100 | let buf = (&mut self.buf) as *mut PhantomData as *mut T; 101 | slice::from_raw_parts_mut(buf, self.len) 102 | } 103 | } 104 | } --------------------------------------------------------------------------------