File: README.md

package info (click to toggle)
rust-unchecked-index 0.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 112 kB
  • sloc: makefile: 4
file content (72 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

# unchecked-index

Unchecked indexing through the regular index syntax.

Using a wrapper type that requires an `unsafe` block to create.

- crates.io: https://crates.io/crates/unchecked-index
- travis: https://travis-ci.org/bluss/unchecked-index

*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.

## Example

```rust

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;
    }
}
```

## How to contribute:

- Fix a bug or implement a new thing
- Include tests for your new feature
- Make a pull request


## Recent Changes

- 0.2.2

  - The crate is now always `no_std`.

- 0.2.1

  - Improve the (debug) assertion messages; fix a typo and always include
    the relevant quantities (start, end, length)

- 0.2.0

  - Add support for unchecked indexing with ranges (“slicing”)
  - Add two free functions, `get_unchecked` and `get_unchecked_mut`

- 0.1.1

  - Add `Copy` impl (for shared slices)

- 0.1.0

  - Initial release