Skip to content

make everything const #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ output directly).

## Supports:

* Scancode Set 1 and 2
* Dvorak 104-key layout
* US 104-key layout
* UK 105-key layout
* JIS 109-key layout
* Azerty full layout
- Scancode Set 1 and 2
- Dvorak 104-key layout
- US 104-key layout
- UK 105-key layout
- JIS 109-key layout
- Azerty full layout

## Usage

Expand Down Expand Up @@ -40,15 +40,15 @@ fn main() {

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.40 and up. It might compile with older versions but that may change in any new patch release.
This crate is guaranteed to compile on stable Rust 1.61 and up. It might compile with older versions but that may change in any new patch release.

## License

Licensed under either of

- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

Expand All @@ -61,8 +61,8 @@ any additional terms or conditions.
## Code of Conduct

Contribution to this crate is organized under the terms of the [Rust Code of
Conduct][CoC], the maintainer of this crate, the [Rust Embedded Community][team], promises
Conduct][coc], the maintainer of this crate, the [Rust Embedded Community][team], promises
to intervene to uphold that code of conduct.

[CoC]: https://www.rust-lang.org/policies/code-of-conduct
[coc]: https://www.rust-lang.org/policies/code-of-conduct
[team]: https://github.com/orgs/rust-embedded-community/teams/all
136 changes: 43 additions & 93 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@
//! that the interrupt can access, which is unsafe.

#![cfg_attr(not(test), no_std)]
#[cfg(test)]
extern crate std as core;

// ****************************************************************************
//
// Imports
//
// ****************************************************************************

#[cfg(not(test))]
use core::marker::PhantomData;

#[cfg(test)]
use std::marker::PhantomData;

// ****************************************************************************
//
// Modules
Expand Down Expand Up @@ -314,7 +312,7 @@ where
S: ScancodeSet,
{
/// Make a new Keyboard object with the given layout.
pub fn new(_layout: T, _set: S, handle_ctrl: HandleControl) -> Keyboard<T, S> {
pub const fn new(handle_ctrl: HandleControl) -> Keyboard<T, S> {
Keyboard {
register: 0,
num_bits: 0,
Expand All @@ -340,7 +338,7 @@ where
}

/// Get the current Ctrl key mapping.
pub fn get_ctrl_handling(&self) -> HandleControl {
pub const fn get_ctrl_handling(&self) -> HandleControl {
self.handle_ctrl
}

Expand Down Expand Up @@ -490,16 +488,16 @@ where
}
}

fn get_bit(word: u16, offset: usize) -> bool {
const fn get_bit(word: u16, offset: usize) -> bool {
((word >> offset) & 0x0001) != 0
}

fn has_even_number_bits(data: u8) -> bool {
const fn has_even_number_bits(data: u8) -> bool {
(data.count_ones() % 2) == 0
}

/// Check 11-bit word has 1 start bit, 1 stop bit and an odd parity bit.
fn check_word(word: u16) -> Result<u8, Error> {
const fn check_word(word: u16) -> Result<u8, Error> {
let start_bit = Self::get_bit(word, 0);
let parity_bit = Self::get_bit(word, 9);
let stop_bit = Self::get_bit(word, 10);
Expand All @@ -525,7 +523,7 @@ where
}

impl KeyEvent {
pub fn new(code: KeyCode, state: KeyState) -> KeyEvent {
pub const fn new(code: KeyCode, state: KeyState) -> KeyEvent {
KeyEvent { code, state }
}
}
Expand All @@ -537,15 +535,15 @@ impl KeyEvent {
// ****************************************************************************

impl Modifiers {
pub fn is_shifted(&self) -> bool {
pub const fn is_shifted(&self) -> bool {
self.lshift | self.rshift
}

pub fn is_ctrl(&self) -> bool {
pub const fn is_ctrl(&self) -> bool {
self.lctrl | self.rctrl
}

pub fn is_caps(&self) -> bool {
pub const fn is_caps(&self) -> bool {
(self.lshift | self.rshift) ^ self.capslock
}
}
Expand All @@ -564,11 +562,8 @@ mod test {

#[test]
fn test_f9() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
// start
assert_eq!(k.add_bit(false), Ok(None));
// 8 data bits (LSB first)
Expand All @@ -591,11 +586,8 @@ mod test {

#[test]
fn test_f9_word() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.add_word(0x0402),
Ok(Some(KeyEvent::new(KeyCode::F9, KeyState::Down)))
Expand All @@ -604,11 +596,8 @@ mod test {

#[test]
fn test_f9_byte() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.add_byte(0x01),
Ok(Some(KeyEvent::new(KeyCode::F9, KeyState::Down)))
Expand All @@ -617,11 +606,8 @@ mod test {

#[test]
fn test_keyup_keydown() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.add_byte(0x01),
Ok(Some(KeyEvent::new(KeyCode::F9, KeyState::Down)))
Expand All @@ -639,11 +625,8 @@ mod test {

#[test]
fn test_f5() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
// start
assert_eq!(k.add_bit(false), Ok(None));
// 8 data bits (LSB first)
Expand All @@ -666,11 +649,8 @@ mod test {

#[test]
fn test_f5_up() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
// Send F0

// start
Expand Down Expand Up @@ -713,11 +693,8 @@ mod test {

#[test]
fn test_shift() {
let mut k = Keyboard::new(
layouts::Uk105Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Uk105Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
// A with left shift held
assert_eq!(
k.process_keyevent(KeyEvent::new(KeyCode::ShiftLeft, KeyState::Down)),
Expand Down Expand Up @@ -815,11 +792,8 @@ mod test {

#[test]
fn test_ctrl() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
// Normal
assert_eq!(
k.process_keyevent(KeyEvent::new(KeyCode::A, KeyState::Down)),
Expand Down Expand Up @@ -876,11 +850,8 @@ mod test {

#[test]
fn test_numlock() {
let mut k = Keyboard::new(
layouts::Uk105Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Uk105Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);

// Numlock ON by default
assert_eq!(
Expand Down Expand Up @@ -946,11 +917,8 @@ mod test {

#[test]
fn test_set_1_down_up_down() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet1,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet1>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.add_byte(0x1e),
Ok(Some(KeyEvent::new(KeyCode::A, KeyState::Down)))
Expand All @@ -967,11 +935,8 @@ mod test {

#[test]
fn test_set_1_ext_down_up_down() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet1,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet1>::new(HandleControl::MapLettersToUnicode);
assert_eq!(k.add_byte(0xe0), Ok(None));
assert_eq!(
k.add_byte(0x1c),
Expand All @@ -986,11 +951,8 @@ mod test {

#[test]
fn test_set_2_poweron() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.add_byte(0xAA),
Ok(Some(KeyEvent::new(
Expand All @@ -1002,11 +964,8 @@ mod test {

#[test]
fn test_set_2_toomanykeys() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.add_byte(0x00),
Ok(Some(KeyEvent::new(
Expand All @@ -1018,11 +977,8 @@ mod test {

#[test]
fn test_set_2_down_up() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.add_byte(0x29),
Ok(Some(KeyEvent::new(KeyCode::Spacebar, KeyState::Down)))
Expand Down Expand Up @@ -1054,11 +1010,8 @@ mod test {

#[test]
fn test_set_2_ext_down_up() {
let mut k = Keyboard::new(
layouts::Us104Key,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(k.add_byte(0xE0), Ok(None));
assert_eq!(
k.add_byte(0x6C),
Expand All @@ -1074,11 +1027,8 @@ mod test {

#[test]
fn test_frazert() {
let mut k = Keyboard::new(
layouts::Azerty,
ScancodeSet2,
HandleControl::MapLettersToUnicode,
);
let mut k =
Keyboard::<layouts::Azerty, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
assert_eq!(
k.process_keyevent(KeyEvent::new(KeyCode::NumpadSlash, KeyState::Down)),
Some(DecodedKey::Unicode('/'))
Expand Down