Skip to main content

tailvore_examples/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3extern crate alloc;
4use alloc::boxed::Box;
5
6use tailvore::self_referencing;
7
8#[cfg(test)]
9mod ok_tests;
10
11pub struct Ext<'this, T, const REV: bool>(&'this Box<T>);
12
13#[self_referencing(pub_extras)]
14pub struct WithConstParam<T: 'static, const REV: bool> {
15    data: core::cell::RefCell<T>,
16    #[borrows(data)]
17    #[not_covariant]
18    dref: Option<Box<Ext<'this, T, REV>>>,
19}
20
21#[self_referencing]
22/// A simple struct which contains an `i32` and a `&'this i32`.
23pub struct DataAndRef {
24    data: i32,
25    #[borrows(data)]
26    data_ref: &'this i32,
27}
28
29#[self_referencing()]
30#[allow(clippy::redundant_allocation)]
31/// A chain of references, where c references b which references a.
32pub struct Chain {
33    a: i32,
34    #[borrows(a)]
35    b: &'this i32,
36    #[borrows(b)]
37    c: &'this i32,
38}
39
40#[self_referencing]
41/// The example provided in the documentation.
42pub struct DocumentationExample {
43    int_data: i32,
44    float_data: f32,
45    #[borrows(int_data)]
46    int_reference: &'this i32,
47    #[borrows(mut float_data)]
48    float_reference: &'this mut f32,
49}
50
51#[self_referencing(no_doc)]
52/// This struct is created using `#[self_referencing(no_doc)]` so the generated methods and
53/// builders are hidden from documentation.
54pub struct Undocumented {
55    data: Box<i32>,
56    #[borrows(data)]
57    data_ref: &'this i32,
58}
59
60/// This struct demonstrates how visibility can be controlled. The struct
61/// is defined with the following code:
62/// ```rust
63/// # use tailvore::self_referencing;
64/// #[self_referencing(pub_extras)]
65/// pub struct Visibility {
66///     private_field: Box<i32>,
67///     #[borrows(private_field)]
68///     pub public_field: &'this i32,
69///     #[borrows(private_field)]
70///     pub(crate) pub_crate_field: &'this i32,
71/// }
72/// ```
73/// By using `pub_extras`, the visibility of items not related to any particular
74/// field like `with_mut` or `VisibilityBuilder` is made public to match the
75/// visibility of the original struct definition. Without adding this option,
76/// these items would only be visible in the module where the struct is
77/// declared.
78#[self_referencing(pub_extras)]
79pub struct Visibility {
80    private_field: Box<i32>,
81    #[borrows(private_field)]
82    pub public_field: &'this i32,
83    #[borrows(private_field)]
84    pub(crate) pub_crate_field: &'this i32,
85}