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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! # Overview
//!
//! `self_cell` provides one macro-rules macro: [`self_cell`]. With this macro you
//! can create self-referential structs that are safe-to-use in stable Rust,
//! without leaking the struct internal lifetime.
//!
//! In a nutshell, the API looks *roughly* like this:
//!
//! ```ignore
//! // User code:
//!
//! self_cell!(
//!     struct NewStructName {
//!         #[from]
//!         owner: Owner,
//!
//!         #[covariant]
//!         dependent: Dependent,
//!     }
//!     
//!     impl {Debug}
//! );
//!
//! // Generated by macro:
//!
//! struct NewStructName(...);
//!
//! impl NewStructName {
//!     fn new(owner: Owner) -> NewStructName { ... }
//!     fn borrow_owner<'a>(&'a self) -> &'a Owner { ... }
//!     fn borrow_dependent<'a>(&'a self) -> &'a Dependent<'a> { ... }
//! }
//!
//! impl Debug for NewStructName { ... }
//! ```
//!
//! Self-referential structs are currently not supported with safe vanilla Rust.
//! The only reasonable safe alternative is to expect the user to juggle 2
//! separate data structures which is a mess. The library solution ouroboros is
//! really expensive to compile due to its use of procedural macros.
//!
//! This alternative is `no_std`, uses no proc-macros, some self contained
//! unsafe and works on stable Rust, and is miri tested. With a total of less
//! than 300 lines of implementation code, which consists mostly of type and
//! trait implementations, this crate aims to be a good minimal solution to the
//! problem of self-referential structs.
//!
//! 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)
//! from experienced Rust users.
//!
//! ### Fast compile times
//!
//! ```ignore
//! $ rm -rf target && cargo +nightly build -Z timings
//!
//! Compiling self_cell v0.7.0
//! Completed self_cell v0.7.0 in 0.2s
//! ```
//!
//! Because it does **not** use proc-macros, and has 0 dependencies
//! compile-times are fast.
//!
//! Measurements done on a slow laptop.
//!
//! ### A motivating use case
//!
//! ```rust
//! use self_cell::self_cell;
//!
//! #[derive(Debug, Eq, PartialEq)]
//! struct Ast<'a>(pub Vec<&'a str>);
//!
//! impl<'a> From<&'a String> for Ast<'a> {
//!     fn from(code: &'a String) -> Self {
//!         // Placeholder for expensive parsing.
//!         Ast(code.split(' ').filter(|word| word.len() > 1).collect())
//!     }
//! }
//!
//! self_cell!(
//!     struct AstCell {
//!         #[from]
//!         owner: String,
//!
//!         #[covariant]
//!         dependent: Ast,
//!     }
//!
//!     impl {Clone, Debug, Eq, PartialEq}
//! );
//!
//! fn build_ast_cell(code: &str) -> AstCell {
//!     // Create owning String on stack.
//!     let pre_processed_code = code.trim().to_string();
//!
//!     // Move String into AstCell, build Ast by calling pre_processed_code.into()
//!     // and then return the AstCell.
//!     AstCell::new(pre_processed_code)
//! }
//!
//! fn main() {
//!     let ast_cell = build_ast_cell("fox = cat + dog");
//!     dbg!(&ast_cell);
//!     dbg!(ast_cell.borrow_owner());
//!     dbg!(ast_cell.borrow_dependent().0[1]);
//! }
//! ```
//!
//! ```txt
//! $ cargo run
//!
//! [src/main.rs:33] &ast_cell = AstCell { owner: "fox = cat + dog", dependent: Ast(["fox", "cat", "dog"]) }
//! [src/main.rs:34] ast_cell.borrow_owner() = "fox = cat + dog"
//! [src/main.rs:35] ast_cell.borrow_dependent().0[1] = "cat"
//! ```
//!
//! There is no way in safe Rust to have an API like `build_ast_cell`, as soon
//! as `Ast` depends on stack variables like `pre_processed_code` you can't
//! return the value out of the function anymore. You could move the
//! pre-processing into the caller but that gets ugly quickly because you can't
//! encapsulate things anymore. Note this is a somewhat niche use case,
//! self-referential structs should only be used when there is no good
//! alternative.
//!
//! Under the hood, it heap allocates a struct which it initializes first by
//! moving the owner value to it and then using the reference to this now
//! Pin/Immovable owner to construct the dependent inplace next to it. This
//! makes it safe to move the generated SelfCell but you have to pay for the
//! heap allocation.
//!
//! See the documentation for [`self_cell`] to dive further into the details.
//!
//! Or take a look at the advanced examples:
//! - [Example how to handle dependent construction that can fail](https://github.com/Voultapher/self_cell/tree/main/examples/fallible_dependent_construction)
//!
//! - [How to build a lazy AST with self_cell](https://github.com/Voultapher/self_cell/tree/main/examples/lazy_ast)
//!
//! - [How to avoid leaking memory if `Dependen::from(&Owner)` panics](https://github.com/Voultapher/self_cell/tree/main/examples/no_leak_panic)

#![no_std]

#[doc(hidden)]
pub extern crate alloc;

#[doc(hidden)]
pub mod unsafe_self_cell;

#[doc(hidden)]
#[macro_export]
macro_rules! _cell_constructor {
    (from, $Vis:vis, $Owner:ty, $Dependent:ident) => {
        $Vis fn new(owner: $Owner) -> Self {
            unsafe {
                // All this has to happen here, because there is not good way
                // of passing the appropriate logic into UnsafeSelfCell::new
                // short of assuming Dependent<'static> is the same as
                // Dependent<'a>, which I'm not confident is safe.

                type JoinedCell<'a> = $crate::unsafe_self_cell::JoinedCell<$Owner, $Dependent<'a>>;

                let layout = $crate::alloc::alloc::Layout::new::<JoinedCell>();

                let joined_void_ptr = $crate::alloc::alloc::alloc(layout);

                let joined_ptr = core::mem::transmute::<*mut u8, *mut JoinedCell>(joined_void_ptr);

                // Move owner into newly allocated space.
                core::ptr::addr_of_mut!((*joined_ptr).owner).write(owner);

                // Initialize dependent with owner reference in final place.
                core::ptr::addr_of_mut!((*joined_ptr).dependent)
                    .write(core::convert::Into::into((&(*joined_ptr).owner)));

                Self {
                    unsafe_self_cell: $crate::unsafe_self_cell::UnsafeSelfCell::new(
                        joined_void_ptr,
                    ),
                }
            }
        }
    };
    (try_from, $Vis:vis, $Owner:ty, $Dependent:ident) => {
        $Vis fn try_from<'a>(
            owner: $Owner,
        ) -> Result<Self, <&'a $Owner as core::convert::TryInto<$Dependent<'a>>>::Error> {
            unsafe {
                // All this has to happen here, because there is not good way
                // of passing the appropriate logic into UnsafeSelfCell::new
                // short of assuming Dependent<'static> is the same as
                // Dependent<'a>, which I'm not confident is safe.

                type JoinedCell<'a> = $crate::unsafe_self_cell::JoinedCell<$Owner, $Dependent<'a>>;

                let layout = $crate::alloc::alloc::Layout::new::<JoinedCell>();

                let joined_void_ptr = $crate::alloc::alloc::alloc(layout);

                let joined_ptr = core::mem::transmute::<*mut u8, *mut JoinedCell>(joined_void_ptr);

                // Move owner into newly allocated space.
                core::ptr::addr_of_mut!((*joined_ptr).owner).write(owner);

                type Error<'a> = <&'a $Owner as core::convert::TryInto<$Dependent<'a>>>::Error;

                // Attempt to initialize dependent with owner reference in final place.
                let try_inplace_init = || -> Result<(), Error<'a>> {
                    core::ptr::addr_of_mut!((*joined_ptr).dependent)
                        .write(core::convert::TryInto::try_into(&(*joined_ptr).owner)?);

                    Ok(())
                };

                match try_inplace_init() {
                    Ok(()) => Ok(Self {
                        unsafe_self_cell: $crate::unsafe_self_cell::UnsafeSelfCell::new(
                            joined_void_ptr,
                        ),
                    }),
                    Err(err) => {
                        // Clean up partially initialized joined_cell.
                        core::ptr::drop_in_place(core::ptr::addr_of_mut!((*joined_ptr).owner));

                        $crate::alloc::alloc::dealloc(joined_void_ptr, layout);

                        Err(err)
                    }
                }
            }
        }
    };
    (from_fn, $Vis:vis, $Owner:ty, $Dependent:ident) => {
        $Vis fn from_fn(
            owner: $Owner,
            dependent_builder: impl for<'a> FnOnce(&'a $Owner) -> $Dependent<'a>
        ) -> Self {
            unsafe {
                // All this has to happen here, because there is not good way
                // of passing the appropriate logic into UnsafeSelfCell::new
                // short of assuming Dependent<'static> is the same as
                // Dependent<'a>, which I'm not confident is safe.

                // For this API to be safe there has to be no safe way to
                // capture additional references in `dependent_builder` and then
                // return them as part of Dependent. Eg. it should be impossible
                // to express: 'a should outlive 'x here `fn
                // bad<'a>(outside_ref: &'a String) -> impl for<'x> FnOnce(&'x
                // Owner) -> Dependent<'x>`.

                // Also because we don't want to store the FnOnce, using this
                // ctor means Clone can't be automatically implemented.

                type JoinedCell<'a> = $crate::unsafe_self_cell::JoinedCell<$Owner, $Dependent<'a>>;

                let layout = $crate::alloc::alloc::Layout::new::<JoinedCell>();

                let joined_void_ptr = $crate::alloc::alloc::alloc(layout);

                let joined_ptr = core::mem::transmute::<*mut u8, *mut JoinedCell>(joined_void_ptr);

                // Move owner into newly allocated space.
                core::ptr::addr_of_mut!((*joined_ptr).owner).write(owner);

                // Initialize dependent with owner reference in final place.
                core::ptr::addr_of_mut!((*joined_ptr).dependent)
                    .write(dependent_builder((&(*joined_ptr).owner)));

                Self {
                    unsafe_self_cell: $crate::unsafe_self_cell::UnsafeSelfCell::new(
                        joined_void_ptr,
                    ),
                }
            }
        }
    };
    (try_from_fn, $Vis:vis, $Owner:ty, $Dependent:ident) => {
        $Vis fn try_from_fn<Err>(
            owner: $Owner,
            dependent_builder: impl for<'a> FnOnce(&'a $Owner) -> Result<$Dependent<'a>, Err>
        ) -> Result<Self, Err> {
            unsafe {
                // All this has to happen here, because there is not good way
                // of passing the appropriate logic into UnsafeSelfCell::new
                // short of assuming Dependent<'static> is the same as
                // Dependent<'a>, which I'm not confident is safe.

                type JoinedCell<'a> = $crate::unsafe_self_cell::JoinedCell<$Owner, $Dependent<'a>>;

                let layout = $crate::alloc::alloc::Layout::new::<JoinedCell>();

                let joined_void_ptr = $crate::alloc::alloc::alloc(layout);

                let joined_ptr = core::mem::transmute::<*mut u8, *mut JoinedCell>(joined_void_ptr);

                // Move owner into newly allocated space.
                core::ptr::addr_of_mut!((*joined_ptr).owner).write(owner);

                type Error<'a> = <&'a $Owner as core::convert::TryInto<$Dependent<'a>>>::Error;

                // Attempt to initialize dependent with owner reference in final place.
                let try_inplace_init = || -> Result<(), Err> {
                    core::ptr::addr_of_mut!((*joined_ptr).dependent)
                        .write(dependent_builder(&(*joined_ptr).owner)?);

                    Ok(())
                };

                match try_inplace_init() {
                    Ok(()) => Ok(Self {
                        unsafe_self_cell: $crate::unsafe_self_cell::UnsafeSelfCell::new(
                            joined_void_ptr,
                        ),
                    }),
                    Err(err) => {
                        // Clean up partially initialized joined_cell.
                        core::ptr::drop_in_place(core::ptr::addr_of_mut!((*joined_ptr).owner));

                        $crate::alloc::alloc::dealloc(joined_void_ptr, layout);

                        Err(err)
                    }
                }
            }
        }
    };
    ($x:ident, $Vis:vis, $Owner:ty, $Dependent:ident) => {
        compile_error!("This macro only accepts `from`, `try_from`, `from_fn` or `try_from_fn`");
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _covariant_access {
    (covariant, $Vis:vis, $Dependent:ident) => {
        $Vis fn borrow_dependent<'a>(&'a self) -> &'a $Dependent<'a> {
            fn _assert_covariance<'x: 'y, 'y>(x: $Dependent<'x>) -> $Dependent<'y> {
                //  This function only compiles for covariant types.
                x // Change the macro invocation to not_covariant.
            }

            unsafe { self.unsafe_self_cell.borrow_dependent() }
        }
    };
    (not_covariant, $Vis:vis, $Dependent:ident) => {
        // For types that are not covariant it's unsafe to allow
        // returning direct references.
        // For example a lifetime that is too short could be chosen:
        // See https://github.com/Voultapher/self_cell/issues/5
    };
    ($x:ident, $Vis:vis, $Dependent:ident) => {
        compile_error!("This macro only accepts `covariant` or `not_covariant`");
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _impl_automatic_derive {
    (Clone, $StructName:ident) => {
        impl Clone for $StructName {
            fn clone(&self) -> Self {
                // TODO support try_from.
                Self::new(self.borrow_owner().clone())
            }
        }
    };
    (Debug, $StructName:ident) => {
        impl core::fmt::Debug for $StructName {
            fn fmt(&self, fmt: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
                self.with_dependent(|owner, dependent| {
                    write!(
                        fmt,
                        concat!(
                            stringify!($StructName),
                            " {{ owner: {:?}, dependent: {:?} }}"
                        ),
                        owner, dependent
                    )
                })
            }
        }
    };
    (PartialEq, $StructName:ident) => {
        impl PartialEq for $StructName {
            fn eq(&self, other: &Self) -> bool {
                *self.borrow_owner() == *other.borrow_owner()
            }
        }
    };
    (Eq, $StructName:ident) => {
        // TODO this should only be allowed if owner is Eq.
        impl Eq for $StructName {}
    };
    (Hash, $StructName:ident) => {
        impl core::hash::Hash for $StructName {
            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
                self.borrow_owner().hash(state);
            }
        }
    };
    ($x:ident, $StructName:ident) => {
        compile_error!(concat!(
            "No automatic trait impl for trait: ",
            stringify!($x)
        ));
    };
}

/// This macro declares a new struct of `$StructName` and implements traits
/// based on `$AutomaticDerive`.
///
/// Example:
/// ```rust
/// use self_cell::self_cell;
///
/// #[derive(Debug, Eq, PartialEq)]
/// struct Ast<'a>(pub Vec<&'a str>);
///
/// impl<'a> From<&'a String> for Ast<'a> {
///     fn from(body: &'a String) -> Self {
///         Ast(vec![&body[2..5], &body[1..3]])
///     }
/// }
///
/// self_cell!(
///     #[doc(hidden)]
///     struct PackedAstCell {
///         #[from]
///         owner: String,
///
///         #[covariant]
///         dependent: Ast,
///     }
///
///     impl {Clone, Debug, PartialEq, Eq, Hash}
/// );
/// ```
///
/// See the crate overview to get a get an overview and a motivating example.
///
/// ### Parameters:
///
/// - `$Vis:vis struct $StructName:ident` Name of the struct that will be
///   declared, this needs to be unique for the relevant scope. Example: `struct
///   AstCell` or `pub struct AstCell`.
///
///   `$(#[$StructMeta:meta])*` allows you specify further meta items for this
///   struct, eg. `#[doc(hidden)] struct AstCell`.
///
/// - `$ConstructorType:ident` Marker declaring if a regular `::new` or
///   `::try_from` constructor should be generated. Possible Values:
///   * **from**: This generates a `fn new(owner: $Owner) -> Self` constructor.
///     For this to work `<&'a $Owner>::Into<$Dependent<'a>>` has to be
///     implemented.
///
///   * **try_from**: This generates a `fn try_from<'a>(owner: $Owner) ->
///     Result<Self, <&'a $Owner as TryInto<$Dependent<'a>>>::Error>`
///     constructor, which allows fallible construction without having to check
///     for failure every time dependent is accessed. For this to work `<&'a
///     $Owner>::TryInto<$Dependent<'a>>` has to be implemented.
///
///   * **from_fn**: This generates a `fn from_fn(owner: $Owner,
///     dependent_builder: impl for<'a> FnOnce(&'a $Owner) -> $Dependent<'a>) ->
///     Self` constructor, which allows more flexible construction that can also
///     return additional unrelated state. But has the drawback of preventing
///     Clone from being automatically implemented. A Fn object would have to
///     be stored to enable this. However you can still implement Clone
///     yourself.
///
///   * **try_from_fn**: This generates a `fn try_from_fn<Err>(owner: $Owner,
///     dependent_builder: impl for<'a> FnOnce(&'a $Owner) ->
///     Result<$Dependent<'a>, Err>) -> Result<Self, Err>` constructor, which
///     allows more flexible fallible construction that can also return
///     additional unrelated state. But has the drawback of preventing Clone
///     from being automatically implemented. A Fn object would have to be
///     stored to enable this. However you can still implement Clone yourself.
///
///   NOTE: If `<&'a $Owner>::Into<$Dependent<'a>>` panics, the value of owner
///   and a heap struct will be leaked. This is safe, but might not be what you
///   want. See [How to avoid leaking memory if `Dependent::from(&Owner)`
///   panics](https://github.com/Voultapher/self_cell/tree/main/examples/no_leak_panic).
///
/// - `$Owner:ty` Type of owner. This has to have a `'static` lifetime. Example:
///   `String`.
///
/// - `$Dependent:ident` Name of the dependent type without specified lifetime.
///   This can't be a nested type name. As workaround either create a type alias
///   `type Dep<'a> = Option<Vec<&'a str>>;` or create a new-type `struct
///   Dep<'a>(Option<Vec<&'a str>>);`. Example: `Ast`.
///
///   `$Covariance:ident` Marker declaring if `$Dependent` is
///   [covariant](https://doc.rust-lang.org/nightly/nomicon/subtyping.html).
///   Possible Values:
///
///   * **covariant**: This generates the direct reference accessor function `fn
///     borrow_dependent<'a>(&'a self) -> &'a $Dependent<'a>`. This is only safe
///     to do if this compiles `fn _assert_covariance<'x: 'y, 'y>(x:
///     $Dependent<'x>) -> $Dependent<'y> { x }`. Otherwise you could choose a
///     lifetime that is too short for types with interior mutability like
///     `Cell`, which can lead to UB in safe code. Which would violate the
///     promise of this library that it is safe-to-use. If you accidentally mark
///     a type that is not covariant as covariant, you will get a compile time
///     error.
///
///   * **not_covariant**: This generates no additional code but you can use `fn
///     with_dependent<Ret>(&self, func: impl for<'a> FnOnce(&'a $Owner, &'a
///     $Dependent<'a>) -> Ret) -> Ret`. See [How to build a lazy AST with
///     self_cell](https://github.com/Voultapher/self_cell/tree/main/examples/lazy_ast)
///     for a usage example.
///
///   In both cases you can use the `fn with_dependent_mut<Ret>(&mut self, func:
///   impl for<'a> FnOnce(&'a $Owner, &'a mut $Dependent<'a>) -> Ret) -> Ret`
///   function to mutate the dependent value. This is safe to do because
///   notionally you are replacing pointers to a value not the other way around.
///
///
/// - `impl {$($AutomaticDerive:ident),*},` Optional comma separated list of
///   optional automatic trait implementations. Possible Values:
///   * **Clone**: Logic `cloned_owner = owner.clone()` and then calls
///     `cloned_owner.into()` to create cloned SelfCell.
///
///   * **Debug**: Prints the debug representation of owner and dependent.
///     Example: `AstCell { owner: "fox = cat + dog", dependent: Ast(["fox",
///     "cat", "dog"]) }`
///
///   * **PartialEq**: Logic `*self.borrow_owner() == *other.borrow_owner()`,
///     this assumes that `Dependent<'a>::From<&'a Owner>` is deterministic, so
///     that only comparing owner is enough.
///
///   * **Eq**: Will implement the trait marker `Eq` for `$StructName`. Beware
///     if you select this `Eq` will be implemented regardless if `$Owner`
///     implements `Eq`, that's an unfortunate technical limitation.
///
///   * **Hash**: Logic `self.borrow_owner().hash(state);`, this assumes that
///     `Dependent<'a>::From<&'a Owner>` is deterministic, so that only hashing
///     owner is enough.
///
///   All `AutomaticDerive` are optional and you can implement you own version
///   of these traits. The declared struct is part of your module and you are
///   free to implement any trait in any way you want. Access to the unsafe
///   internals is only possible via unsafe functions, so you can't accidentally
///   use them in safe code.
///
#[macro_export]
macro_rules! self_cell {
    (
        $(#[$StructMeta:meta])*
        $Vis:vis struct $StructName:ident {
            #[$ConstructorType:ident]
            owner: $Owner:ty,

            #[$Covariance:ident]
            dependent: $Dependent:ident,
        }

        $(impl {$($AutomaticDerive:ident),*})?
    ) => {
        $(#[$StructMeta])*
        $Vis struct $StructName {
            unsafe_self_cell: $crate::unsafe_self_cell::UnsafeSelfCell<
                $Owner,
                $Dependent<'static>
            >
        }

        impl $StructName {
            $crate::_cell_constructor!($ConstructorType, $Vis, $Owner, $Dependent);

            $Vis fn borrow_owner<'a>(&'a self) -> &'a $Owner {
                unsafe { self.unsafe_self_cell.borrow_owner::<$Dependent<'a>>() }
            }

            $Vis fn with_dependent<Ret>(&self, func: impl for<'a> FnOnce(&'a $Owner, &'a $Dependent<'a>) -> Ret) -> Ret {
                unsafe {
                    func(
                        self.unsafe_self_cell.borrow_owner::<$Dependent>(),
                        self.unsafe_self_cell.borrow_dependent()
                    )
                }
            }

            $Vis fn with_dependent_mut<Ret>(&mut self, func: impl for<'a> FnOnce(&'a $Owner, &'a mut $Dependent<'a>) -> Ret) -> Ret {
                let joined_cell = unsafe {
                     self.unsafe_self_cell.borrow_mut()
                };

                func(&joined_cell.owner, &mut joined_cell.dependent)
            }

            $crate::_covariant_access!($Covariance, $Vis, $Dependent);
        }

        impl Drop for $StructName {
            fn drop<'a>(&mut self) {
                unsafe {
                    self.unsafe_self_cell.drop_joined::<$Dependent>();
                }
            }
        }

        // The user has to choose which traits can and should be automatically
        // implemented for the cell.
        $($(
            $crate::_impl_automatic_derive!($AutomaticDerive, $StructName);
        )*)*
    };
}