Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bodil/array-ops
Ready made default method implementations for array data types.
https://github.com/bodil/array-ops
Last synced: 2 months ago
JSON representation
Ready made default method implementations for array data types.
- Host: GitHub
- URL: https://github.com/bodil/array-ops
- Owner: bodil
- License: mpl-2.0
- Created: 2020-03-19T13:47:37.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-16T06:22:28.000Z (over 3 years ago)
- Last Synced: 2024-10-07T23:49:23.020Z (3 months ago)
- Language: Rust
- Homepage: https://docs.rs/array-ops/
- Size: 18.6 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# array-ops
Ready made default method implementations for array data types.
## Overview
This crate provides a number of traits with default implementations for most of the standard
library's methods on array like data structures. All you need to do to apply them to your own array
like data structure is to implement `HasLength` and `Index` (and `IndexMut` for
mutable operations), which means you need a `len()` method and an `index()` method, and the `Array`
trait will provide default methods for everything else, implemented using just those two methods.## Documentation
- [API docs](https://docs.rs/array-ops)
# Example
```rust
# use array_ops::*;
# use std::ops::{Index, IndexMut};
#[derive(PartialEq, Eq, Debug)]
struct MyNewtypedVec(Vec);impl From> for MyNewtypedVec {
fn from(vec: Vec) -> Self {
Self(vec)
}
}impl HasLength for MyNewtypedVec {
fn len(&self) -> usize {
self.0.len()
}
}impl Index for MyNewtypedVec {
type Output = A;
fn index(&self, index: usize) -> &A {
self.0.index(index)
}
}impl IndexMut for MyNewtypedVec {
fn index_mut(&mut self, index: usize) -> &mut A {
self.0.index_mut(index)
}
}impl Array for MyNewtypedVec {}
impl ArrayMut for MyNewtypedVec {}# fn main() {
let mut my_vec = MyNewtypedVec::from(vec![3, 1, 3, 3, 7]);
assert!(my_vec.starts_with(&[3, 1, 3]));
my_vec.sort_unstable();
let expected = MyNewtypedVec::from(vec![1, 3, 3, 3, 7]);
assert_eq!(expected, my_vec);
# }
```## Licence
Copyright 2020 Bodil Stokke
This software is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL
was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct][coc]. By
participating in this project you agree to abide by its terms.[immutable.rs]: https://immutable.rs/
[coc]: https://github.com/bodil/sized-chunks/blob/master/CODE_OF_CONDUCT.md