Tuplespace - Implementing tuples with wildcards


#1

Greetings!

I’m currently learning Rust and chose to implement a tuple space as an exercise to get a grasp on how Rust treats some common concepts, e.g.: data types, multi-threading and network communication.

What stumped me right away was coming up with a suitable data type for the tuple. It is essentially the same as the one Rust provides, while also requiring additional fields like lifetime and wild card support for pattern matching.

After several failed attempts (is it even possible to have structs with tuples as member?) I discovered the rust LinuxTuples implementation which realizes tuples like this:

/// Enum, which is used to contain LinuxTuples element
#[derive(Clone, PartialEq)]
pub enum E {
    I(i32),
    D(f64),
    S(String),
    T(Vec<E>),
    None,
}

where a tuple is a vector of E and None is used as a wild card.

This seems a very intuitive approach, and one could build a struct like this

pub struct TupleType {
    content: Vec<E>,
    lifetime: u64,
}

and call it a day. Or am I missing something?

Is there any other (elegant) approach to implementing a tuple structure like that in Rust?


#2

Yes. It’s written like this:

struct SomeStruct {
    tuple_member: (String, String),
}

https://play.rust-lang.org/?gist=dfc31f4950c42ffad338aa7703964f90&version=stable


#3

For this you need to know in advance which types the tuple will contain. I was thinking more like

struct SomeStruct {
    tuple_member: Tuple(),
}