![Ascii Art](ascii-art.png)

# 🌾 Campo

Campo is a [no_std](https://docs.rust-embedded.org/book/intro/no-std.html)
library to deal with bit fields and bit masks: it provides an easy interface to
create, modify and work with bits.

The field can be backed by any of the unsigned primitive integer types: `u8`,
`u16`, `u32`, `u64`, `u128`.

```rust
let mut field = Campo::<u16>::new(13.into()).expect("The size of the field exceeds 16 bits");
field.set_unchecked(1.into());

assert_eq!(field.get(0.into()).unwrap(), 0);
assert_eq!(field.get(1.into()).unwrap(), 1);
// Try to get an index beyond the size of the field
assert!(field.get(13.into()).is_err());

// Campo instances can be initialized directly from the backing type (the size
// of the field is assumed to be the maximum for the given type in these cases)
let mask: Campo::<u16> = 0b1100u16.into();
// There's support for bitwise operations (AND, OR and XOR), with other Campo
// instances backed by the same type. You can OR your field with a bitmask (another
// Campo instance), to set multiple bits at once
field |= mask;

// You can also shift the bits of your field
field <<= 3;

// Finally, you can display your Campo instances
println!("Your field is: {}", field);
```

## Vectorized field

It is also possible to generate a field backed by a `Vec<u8>`: to do so you have
to enable the `alloc` feature which will require linking the `std` library for
the memory allocation capability.

```rust
let mut field = Campo::<Vec<u8>>::new(10000.into());

let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
let index: usize = buffer.trim().parse().unwrap();

field.set(index.into()).unwrap();
```
