Skip to content

'static lifetimes in operations #102

@AlexNav73

Description

@AlexNav73

Hi, in my case, i need to use &'static str for index and doc type, but lifetimes in IndexOperation in such case, disallow me pass reference to doc with non-static lifetime. What i try to say:

const ES_INDEX_NAME: &'static str = "my_index";

pub trait EsDocument: Serialize + Deserialize {
    fn entity_type() -> &'static str;
}

pub fn index<'a, 'b, T>(&'a mut self, doc: &'b T) -> &'a mut IndexOperation<'a, 'b, T> 
    where T: EsDocument
{
    self.client.index(ES_INDEX_NAME, T::entity_type()).with_doc(doc)
}

In this example, lifetime 'b == 'static, because:

fn index<'a, 'b, E: Serialize>(&'a mut self, index: &'b str, doc_type: &'b str) -> IndexOperation<'a, 'b, E>
fn with_doc(&'b mut self, doc: &'b E) -> &'b mut Self

with_doc accepts &'b E (in our case &'static E) and passing doc: &'b T for 'b != 'static breaks borrow checker rules

For now, possible solution can be using mem::transmute to avoid this lifetime rules.

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