Gatonito
Gatonito

Reputation: 2154

How to match over Pin<Type> in Rust?

I was trying to do this:

use std::pin::Pin;

enum A{
    AA,
    BB,
    CC
}

fn main() {
    let a = Pin::new(A::AA);
    match a{
        Pin<AA> => {},
        Pin<BB> => {},
        Pin<CC> => {}
    }
}

until I realized I don't understand what's happening. How can I match over the variants inside Pin?

Error:

error: expected one of `=>`, `@`, `if`, or `|`, found `<`
  --> src/main.rs:13:12
   |
13 |         Pin<AA> => {},
   |            ^ expected one of `=>`, `@`, `if`, or `|`

Upvotes: 0

Views: 863

Answers (1)

HJVT
HJVT

Reputation: 2192

First of all, as @SilvioMayolo mentioned, you can't pin your enum directly, because Pin<P> requres P: Deref.
If you fix that by putting your enum in a Box and pinning that, you will be able to match on your pinned value by dereferencing it, because Pin is Deref:

enum A {
    AA,
    BB,
    CC,
}

fn main() {
    let a = Box::pin(A::AA);
    match *a {
        A::AA => {}
        A::BB => {}
        A::CC => {}
    }
}

Playground

Upvotes: 2

Related Questions