Skip to content

Using generic type params #9

@nyurik

Description

@nyurik

Is there a way to include a generic type param?

#[borrowme]
#[derive(Debug)]
pub struct GenericObj<'a, T: Clone> {
    #[borrowme(borrow_with = Vec::as_slice)]
    pub data: &'a [T],
}

or possibly a simpler variant

#[borrowme]
#[derive(Debug)]
pub struct PhantomGenericObj<'a, T> {
    #[borrowme(borrow_with = Vec::as_slice)]
    data: &'a [u8],
    _marker: std::marker::PhantomData<T>,
}

In both cases I get this error:

16 | #[borrowme]
   | ----------- the parameter type `T` must be valid for the lifetime `'this` as defined here...
17 | #[derive(Debug)]
18 | pub struct GenericObj<'a, T> {
   |            ^^^^^^^^^^^^^^^^ ...so that the type `OwnedGenericObj<T>` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound
   |
18 | pub struct GenericObj<'a, T> where T: 'this {
   |                             ++++++++++++++

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions