19 releases (8 breaking)

0.13.0 Oct 10, 2021
0.11.1 Sep 24, 2021
0.10.1 Jul 14, 2021
0.8.3 Mar 14, 2021
0.5.1 Nov 12, 2020

#26 in Procedural macros

Download history 11670/week @ 2021-07-06 11478/week @ 2021-07-13 11420/week @ 2021-07-20 14743/week @ 2021-07-27 13233/week @ 2021-08-03 10853/week @ 2021-08-10 15645/week @ 2021-08-17 15994/week @ 2021-08-24 12401/week @ 2021-08-31 20093/week @ 2021-09-07 18729/week @ 2021-09-14 16804/week @ 2021-09-21 38662/week @ 2021-09-28 51864/week @ 2021-10-05 48248/week @ 2021-10-12 53991/week @ 2021-10-19

80,099 downloads per month
Used in 124 crates (via ouroboros)

MIT/Apache

80KB
2K SLoC

Ouroboros

Ouroboros on Crates.IO Documentation

Easy self-referential struct generation for Rust. Dual licensed under MIT / Apache 2.0.

Note: Version 0.10.0 and later automatically box every field. This is done to prevent undefined behavior, but has the side effect of making the library easier to work with.

Tests are located in the examples/ folder because they need to be in a crate outside of ouroboros for the self_referencing macro to work properly.

use ouroboros::self_referencing;

#[self_referencing]
struct MyStruct {
    int_data: i32,
    float_data: f32,
    #[borrows(int_data)]
    int_reference: &'this i32,
    #[borrows(mut float_data)]
    float_reference: &'this mut f32,
}

fn main() {
    let mut my_value = MyStructBuilder {
        int_data: 42,
        float_data: 3.14,
        int_reference_builder: |int_data: &i32| int_data,
        float_reference_builder: |float_data: &mut f32| float_data,
    }.build();

    // Prints 42
    println!("{:?}", my_value.borrow_int_data());
    // Prints 3.14
    println!("{:?}", my_value.borrow_float_reference());
    // Sets the value of float_data to 84.0
    my_value.with_mut(|fields| {
        **fields.float_reference = (**fields.int_reference as f32) * 2.0;
    });

    // We can hold on to this reference...
    let int_ref = *my_value.borrow_int_reference();
    println!("{:?}", *int_ref);
    // As long as the struct is still alive.
    drop(my_value);
    // This will cause an error!
    // println!("{:?}", *int_ref);
}

Dependencies

~0.5–1MB
~21K SLoC