keyed_vec

Crates.iokeyed_vec
lib.rskeyed_vec
version0.1.0
sourcesrc
created_at2023-08-15 10:55:00.920672
updated_at2023-08-15 10:55:00.920672
descriptionVector type where the key is a custom object to avoid mixing indices between vectors
homepagehttps://github.com/playest/keyed_vec
repositoryhttps://github.com/playest/keyed_vec
max_upload_size
id944905
size52,418
(playest)

documentation

https://github.com/playest/keyed_vec

README

A Vector type where the key is a custom object to avoid mixing indices between vectors.

Example

use keyed_vec::{KeyedVec, IndexLike};

#[derive(Debug, Clone)]
struct Client {
    name: String,
}

#[derive(Debug, Clone)]
struct Order {
    address: String
}

#[derive(Debug, Clone, Copy)]
struct ClientId(usize);
impl IndexLike for ClientId {
    fn to_index(&self) -> usize {
        self.0
    }

    fn from_index(i: usize) -> Self {
        ClientId(i)
    }
}

#[derive(Debug, Clone, Copy)]
struct OrderId(usize);
impl IndexLike for OrderId {
    fn to_index(&self) -> usize {
        self.0
    }

    fn from_index(i: usize) -> Self {
        OrderId(i)
    }
}

let mut clients = KeyedVec::<ClientId, Client>::new();
let client_a = clients.push(Client { name: "A".to_string() });

let mut orders = KeyedVec::<OrderId, Order>::new();
let order_1 = orders.push(Order { address: "1".to_string() });

// mismatched types expected `ClientId`, found `OrderId`
clients.get(order_1);
Commit count: 2

cargo fmt