Returning a value referencing self

Viewed 64

Following the thread of this post: Converting trait object to impl expression

I am implementing the tiberius IntoSql<'a> trait for a trait of mine.

pub trait QueryParameters<'a>: Sync + Send {
    fn as_postgres_param(&self) -> &(dyn ToSql + Sync + 'a);
}

impl<'a> IntoSql<'a> for &'a dyn QueryParameters<'a> {
    fn into_sql(self) -> ColumnData<'a> {
        let cast = &self as &dyn std::any::Any;

        let casted = match cast.type_id() {
            String => match cast.downcast_ref::<String>() {
                Some(v) => ColumnData::String(Some(Cow::from(v.as_str()))),
                None => todo!(),
            },
            i32 => match cast.downcast_ref::<i32>() {
                Some(v) => ColumnData::I32(Some(*v)),
                None => todo!(),
            },
            _ => todo!()
        };

        casted
    }
}

But I have this issue:

lifetime may not live long enough cast requires that 'a must outlive 'static

and this one on the return position:

cannot return value referencing function parameter self returns a value referencing data owned by the current function

I understand that the first is an issue of std::any::Any, which returns a reference with an 'static lifetime.

But, for the second, I tried to own the data, to clone the data... but I really can't figure out how to solve both of them.

How can I solve both issues? And what I am doing wrong?

0 Answers
Related