Stream: general

Topic: Trait for Owned, Rc and Arc


marmeladema (Apr 29 2020 at 18:05, on Zulip):

Hello! I am trying to declare a struct with a generic type which i want to restrict to something can either be an owned value, an rc or an arc (or any things that can give me a reference to my target type).
I've tried using Deref trait, but obvisouly that does not work for the "owned" case.
I pasted my code here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fffb76898fa8b11f38973bcc50074b9a

RalfJ (Apr 29 2020 at 18:05, on Zulip):

doing this properly needs "higher-order traits" aka higher-kinded types aka GAT (generic associated types)

marmeladema (Apr 29 2020 at 18:07, on Zulip):

It seems that every time i want to do fancy trait stuff, i end up being blocked by the lack of GAT :p

RalfJ (Apr 29 2020 at 18:07, on Zulip):

yeah there is a reason people keep asking for them ;)

marmeladema (Apr 29 2020 at 18:09, on Zulip):

If only i had enough knowledge to help on the matter :/
In the mean time, is there a "hack" i could use?

Jake Goulding (Apr 29 2020 at 18:11, on Zulip):

DID SOMEONE SAY GAT?

This is the SO Rust chatroom meta:

image.png

Jake Goulding (Apr 29 2020 at 18:12, on Zulip):

GATs are a bit of a meme over there

Jake Goulding (Apr 29 2020 at 18:12, on Zulip):

@marmeladema Is there any way to simulate Generic Associated Types / Associated Type Constructors in Rust?

Jake Goulding (Apr 29 2020 at 18:13, on Zulip):

There's some small things, but nothing complete

marmeladema (Apr 29 2020 at 18:13, on Zulip):

i'll read that

Jake Goulding (Apr 29 2020 at 18:13, on Zulip):

(or any things that can give me a reference to my target type

what about https://doc.rust-lang.org/std/convert/trait.AsRef.html ?

RalfJ (Apr 29 2020 at 18:14, on Zulip):

@Jake Goulding do you have highlighting configured for GAT here or what? ;)

Jake Goulding (Apr 29 2020 at 18:14, on Zulip):

Or Borrow, if you can figure out the difference.

LeSeulArtichaut (Apr 29 2020 at 18:14, on Zulip):

kirby-dream-land? shepmaster-fanclub? I see what you did there x)

marmeladema (Apr 29 2020 at 18:15, on Zulip):

Same error https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fdc9010cfe53af9e9b0d47e259466b98

Jake Goulding (Apr 29 2020 at 18:15, on Zulip):

@RalfJ amusingly, I'm not the primary GAT memer there. I just join in that fun.

@LeSeulArtichaut And I didn't even do that either ;-)

There was a week where all of the SO regulars had a Kirby-themed avatar. That was highly confusing.

Jake Goulding (Apr 29 2020 at 18:16, on Zulip):

@marmeladema https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=929d0c6b211a88b78459cb9c6312adcf Borrow works

marmeladema (Apr 29 2020 at 18:17, on Zulip):

oh cool

marmeladema (Apr 29 2020 at 18:17, on Zulip):

I guess there is no impl<T> AsRef<T> for T {...}?

marmeladema (Apr 29 2020 at 18:18, on Zulip):

but why?

marmeladema (Apr 29 2020 at 18:19, on Zulip):

Unlike AsRef, Borrow has a blanket impl for any T, and can be used to accept either a reference or a value.

marmeladema (Apr 29 2020 at 18:20, on Zulip):

but good news is, it seems i don't need GAT after all :D

Jake Goulding (Apr 29 2020 at 18:21, on Zulip):

That's also one of the key "tricks" for avoiding GATs — don't be abstract over references or not references.

Jake Goulding (Apr 29 2020 at 18:22, on Zulip):

Since you said you wanted to always get a reference.

marmeladema (Apr 29 2020 at 19:38, on Zulip):

@Jake Goulding i slightly modified your solution by adding a lifetime to Inner: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=802b60d5e5abbc530b8c92a70c44bd57
Compiler complains that the lifetime is not used. Is this intended?

marmeladema (Apr 29 2020 at 19:38, on Zulip):

I mean, I can add a phantomData is that pleases rustc, but i don't really understand why i have to do that

Bastian Kauschke (Apr 29 2020 at 19:49, on Zulip):

I understand it in vague terms which I both can't express cleanly and wouldn't help much :sweat_smile:
I do recommend using PhantomData<Inner<'a>> to please the crab :crab:

Bastian Kauschke (Apr 29 2020 at 19:52, on Zulip):

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7c2fd589dc5e1408359a34feeffccd33

Jake Goulding (Apr 29 2020 at 20:11, on Zulip):

@marmeladema I'd probably just leave that bound off https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9b412eb117e0dc4528db53ad241e2296

marmeladema (Apr 29 2020 at 20:41, on Zulip):

oh right ok, i was making something way more complicated that it needs to be^^

marmeladema (Apr 29 2020 at 20:41, on Zulip):

thanks for the help

Last update: May 29 2020 at 17:40UTC