keyed_vec 0.1.0

Vector type where the key is a custom object to avoid mixing indices between vectors
Documentation
  • Coverage
  • 4.55%
    2 out of 44 items documented0 out of 43 items with examples
  • Size
  • Source code size: 52.41 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 716.23 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Documentation
  • playest/keyed_vec
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • playest

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);