lives-macros

Crates.iolives-macros
lib.rslives-macros
version0.1.2
created_at2026-01-09 18:51:19.80605+00
updated_at2026-01-12 13:17:40.872673+00
descriptionProcedural macros for lives-rs
homepage
repositoryhttps://github.com/aegistudio/lives-rs
max_upload_size
id2032620
size7,970
(aegistudio)

documentation

README

lives-rs

Lifetime dynamic smart pointers.

CI Status MIT licensed

Introduction

This crate aims at making weak pointers that may forget covariant lifetimes of a type. It relies on the fact that object may not outlive its covariant lifetime, and performs runtime checking of the liveness of the pointee object to determine the liveness of the forgotten lifetime. When it's alive, the weak pointer allows the caller to perform actions on a reference to the pointee object borrowed for a lifetime strictly contained in the covariant lifetime of the object, while forbidding the caller from extending it.

This allows us to write:

use lives::{Life, LifeRc, LifeWeak};

#[derive(Life)]
struct Str<'a> (&'a str);

fn main() {
    let mut weaks: Vec<LifeWeak<Str<'static>>> = Vec::new();
    
    {
        let a = format!("{}", 123456);
        let a_rc = LifeRc::new(Str(a.as_str()));
        weaks.push(LifeRc::downgrade(&a_rc));
    }
    
    let b = format!("{}", 789012);
    let b_rc = LifeRc::new(Str(b.as_str()));
    weaks.push(LifeRc::downgrade(&b_rc));
    
    for weak in weaks {
        weak.with(|r| println!("{}", r.0));
    }
}

The standard library Weak pointer will not allow you to do that, since it's otherwise prone to upgrading the weak pointer to an Rc and holding it, extending lifetime for arbitrarily long. Our crate think differently by forbidding the upgrade of a LifeWeak pointer back to LifeRc pointer, and regulating the access of reference to the still alive pointee, by constraining the borrow lifetime.

The introduction above explains the idea but might be oversimplifying the constraints of this crate. For more detail, please see the documentation.

License

This project is licensed under the MIT license.

Commit count: 7

cargo fmt