### Rust Self-Referential Struct Generation with Ouroboros Source: https://github.com/someguynamedjosh/ouroboros/blob/main/README.md Demonstrates the basic usage of the `ouroboros::self_referencing` macro to create a struct with fields that reference other fields within the same struct. It shows how to define the struct, generate a builder, and access fields safely using generated methods like `borrow_int_data()` and `borrow_float_reference()`. The example also illustrates modifying a borrowed field using `with_mut`. ```rust use ouroboros::self_referencing; #[self_referencing] struct MyStruct { int_data: i32, float_data: f32, #[borrows(int_data)] // the 'this lifetime is created by the #[self_referencing] macro // and should be used on all references marked by the #[borrows] macro int_reference: &'this i32, #[borrows(mut float_data)] float_reference: &'this mut f32, } fn main() { // The builder is created by the #[self_referencing] macro // and is used to create the struct let mut my_value = MyStructBuilder { int_data: 42, float_data: 3.14, // Note that the name of the field in the builder // is the name of the field in the struct + `_builder` // ie: {field_name}_builder // the closure that assigns the value for the field will be passed // a reference to the field(s) defined in the #[borrows] macro int_reference_builder: |int_data: &i32| int_data, float_reference_builder: |float_data: &mut f32| float_data, }.build(); // The fields in the original struct can not be accessed directly // The builder creates accessor methods which are called borrow_{field_name}() // 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); } ``` -------------------------------- ### Rust Struct Wrapper Pattern with Ouroboros Source: https://github.com/someguynamedjosh/ouroboros/blob/main/README.md Illustrates a common pattern where the `#[self_referencing]` macro is applied to an internal struct, and a public-facing 'friendly' struct is created to provide a cleaner API. This approach prevents the macro-generated methods from polluting the public interface of a library. ```rust // The extra wrapper methods are only added to this struct. #[self_referencing] struct Internal { // ... } // This struct is free to provide a nicer interface that is not polluted by the // extra functions #[self_referencing] adds. pub struct Friendly { internal: Internal, } impl Friendly { pub fn new(/* ... */) -> Self { // Complicated code here... } pub fn do_the_thing(&self) -> T { // More complicated code here.... } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.