Crates.io | unchecked-index |
lib.rs | unchecked-index |
version | 0.2.2 |
source | src |
created_at | 2017-10-08 12:01:14.103256 |
updated_at | 2017-11-18 15:07:23.04629 |
description | Unchecked indexing wrapper using regular index syntax. |
homepage | |
repository | https://github.com/bluss/unchecked-index |
max_upload_size | |
id | 34832 |
size | 26,971 |
Unchecked indexing through the regular index syntax.
Using a wrapper type that requires an unsafe
block to create.
Note: All unchecked indexing here is actually “checked” with debug
assertions when they are enabled (they are off by default in release
builds). This is a feature! Debug checking does not make your code safe,
but it helps finding bugs in unsafe
code. Test your code responsibly.
use unchecked_index::unchecked_index;
/// unsafe because: trusts the permutation to be correct
unsafe fn apply_permutation<T>(perm: &mut [usize], v: &mut [T]) {
debug_assert_eq!(perm.len(), v.len());
// use unchecked (in reality, debug-checked) indexing throughout
let mut perm = unchecked_index(perm);
for i in 0..perm.len() {
let mut current = i;
while i != perm[current] {
let next = perm[current];
// move element from next to current
v.swap(next, current);
perm[current] = current;
current = next;
}
perm[current] = current;
}
}
Fix a bug or implement a new thing
Include tests for your new feature
Make a pull request
0.2.2
no_std
.0.2.1
0.2.0
get_unchecked
and get_unchecked_mut
0.1.1
Copy
impl (for shared slices)0.1.0