rust-analyzer / countme

Apache License 2.0
18 stars 4 forks source link

parameter type `T` may not live long enough - when the counted struct is generic #13

Open peter-lyons-kehl opened 2 years ago

peter-lyons-kehl commented 2 years ago

Somewhere between v. 2 and 3.0.1 countme changed so the following

#[derive(PartialEq)]
pub struct SegmentedBuf<T> {
    _c: countme::Count<Self>,
    pub(crate) bufs: SmallVec<[T; 4]>,
    pos: usize,
    offset: usize,
    read_pos: usize,
    read_offset: usize,
    segment_size: usize,
}

fails with

error[E0310]: the parameter type `T` may not live long enough
   --> src/segmented_buffer.rs:104:9
    |
103 | pub struct SegmentedBuf<T> {
    |                         - help: consider adding an explicit lifetime bound...: `T: 'static`
104 |     _c: countme::Count<Self>,
    |         ^^^^^^^^^^^^^^^^^^^^ ...so that the type `SegmentedBuf<T>` will meet its required lifetime bounds...
    |
note: ...that is required by this bound
   --> /home/pkehl/.cargo/registry/src/github.com-1ecc6299db9ec823/countme-3.0.1/src/lib.rs:71:21
    |
71  | pub struct Count<T: 'static> {

FYI the error comes from https://github.com/logdna/logdna-rust/blob/main/src/segmented_buffer.rs#L103= (after updating the version in Cargo.toml). I'm not familiar with logdna-rust yet, but I'll try to narrow this issue down. (The same error happens with Rust 1.61.0 and 1.63.0-nightly.)

peter-lyons-kehl commented 2 years ago

Ahah, as per "it means it doesn't/can't contain any non-static references. Meaning it can only have owned types and/or 'static references." (from https://www.reddit.com/r/rust/comments/o9w6rl/comment/h3dszio/?utm_source=reddit&utm_medium=web2x&context=3).

May I suggest a note about that in README.md, please. Especially so because this use of 'static is infrequent, and cargoc's message is not helpful either.