Abstract over all pointers

Hi there!
That was fun until I hit smart pointers.
Here is my structure which holds a reference to some type implementing the trait:

trait Kek {}

struct Pook<'a, K>
where
    K: Kek,
{
    kek_ref: &'a K,
}

How can I abstract over & for kek_ref? I mean what bounds should I set to allow, for instance, Rc or any other pointer for that place?

How about Borrow?

I tried with AsRef but see errors, here is the example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=487b785d0f5654e649e099a976bc7dc8

You can use PhantomData:

struct Pook<'a, K, KR>
where 
    K: Kek,
    KR: AsRef<K> + 'a
{
    kek_ref: KR,
    phantom: PhantomData<&'a K>,
}

playground

2 Likes

oh thank you,
and for your fast replies too

1 Like

Why do you need a lifetime on the struct?

Just wanted something like 1:1 conversion from reference usage to AsRef

I think the closest, and strictest, would be Deref.

In general, though, KR can already carry it's own lifetime. An extra lifetime doesn't serve much of a useful purpose.

I.e. you can have the type, Pook<[u8], &'a str>, which already has an 'a. Having Pook<'a, [u8], &'a str> doesn't really make this any more expressive.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.