Peternator7 / strum

A small rust library for adding custom derives to enums
https://crates.io/crates/strum
MIT License
1.8k stars 151 forks source link

AsRefStr documentation incorrectly states 'static ref is returned #326

Closed DTrippe closed 9 months ago

DTrippe commented 10 months ago

Just wanted to report that the AsRefStr documentation is wrong. It claims it will create a static str reference, which doesn't seem to be true.

Converts enum variants to &'static str.

The lifetime of the reference is tied to the lifetime of the instance itself. E.,g

#[derive(strum::AsRefStr, strum::IntoStaticStr)]
enum MyVariant {
    A
}

fn main() {
    // Compiles fine
    let variant_str: &'static str = {
        let my_variant = MyVariant::A;
        my_variant.into()
    };
    // Complains that my_variant is dropped while still borrowed
    let variant_str: &'static str = {
        let my_variant = MyVariant::A;
        my_variant.as_ref()
    };
}

Just feel the documentation could be more explicit, and direct people to IntoStaticStr

Peternator7 commented 9 months ago

Thank you for reporting this issue. Would you mind opening a PR to correct it?

DTrippe commented 9 months ago

Created a PR here: https://github.com/Peternator7/strum/pull/330

Closing this as a result.