Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/matklad/typed_index_derive
https://github.com/matklad/typed_index_derive
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/matklad/typed_index_derive
- Owner: matklad
- License: apache-2.0
- Created: 2018-06-03T20:53:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-19T13:52:59.000Z (over 6 years ago)
- Last Synced: 2024-09-14T12:52:12.311Z (2 months ago)
- Language: Rust
- Size: 9.77 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# typed_index_derive
[![Build Status](https://travis-ci.org/matklad/typed_index_derive.svg?branch=master)](https://travis-ci.org/matklad/typed_index_derive)
[![Crates.io](https://img.shields.io/crates/v/typed_index_derive.svg)](https://crates.io/crates/typed_index_derive)
[![API reference](https://docs.rs/typed_index_derive/badge.svg)](https://docs.rs/typed_index_derive/)Custom derive to easily create newtype index types.
A frequent pattern in Rust is to store objects in a vector and use integer indexes
as handlers to them. While using `usize` works, it could become confusing if there
are several flavors of indexes. To make the meaning of each index clear the newtype
wrappers like `FooIdx(usize)` are useful, but require a fair amount of boilerplate.
This crate derives the boilerplate for you:```rust
#[macro_use]
extern crate typed_index_derive;struct Spam(String);
#[derive(
// Usual derives for plain old data
Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash,
// this crate
TypedIndex
)]
#[typed_index(Spam)] // index into `&[Spam]`
struct SpamIdx(usize); // could be `u32` instead of `usize`fn main() {
let spams = vec![Spam("foo".into()), Spam("bar".into()), Spam("baz".into())];// Conversions between `usize` and `SpamIdx`
let idx: SpamIdx = 1.into();
assert_eq!(usize::from(idx), 1);// We can index `Vec` with SpamIdx
assert_eq!(&spams[idx].0, "bar");// However, we can't index `Vec`
// vec![1, 2, 3][idx]
// error: slice indices are of type `usize` or ranges of `usize`// You can add/subtract `usize` from an index
assert_eq!(&spams[idx - 1].0, "foo");// The difference between two indices is `usize`
assert_eq!(idx - idx, 0usize);
}
```