Skip to content

Commit 45ea08e

Browse files
authored
Merge pull request #19 from TheBotlyNoob/feat-const
make everything const
2 parents 0089d3b + 9f179f6 commit 45ea08e

File tree

2 files changed

+55
-105
lines changed

2 files changed

+55
-105
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ output directly).
66

77
## Supports:
88

9-
* Scancode Set 1 and 2
10-
* Dvorak 104-key layout
11-
* US 104-key layout
12-
* UK 105-key layout
13-
* JIS 109-key layout
14-
* Azerty full layout
9+
- Scancode Set 1 and 2
10+
- Dvorak 104-key layout
11+
- US 104-key layout
12+
- UK 105-key layout
13+
- JIS 109-key layout
14+
- Azerty full layout
1515

1616
## Usage
1717

@@ -40,15 +40,15 @@ fn main() {
4040

4141
## Minimum Supported Rust Version (MSRV)
4242

43-
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.
43+
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.
4444

4545
## License
4646

4747
Licensed under either of
4848

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

5353
at your option.
5454

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

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

67-
[CoC]: https://www.rust-lang.org/policies/code-of-conduct
67+
[coc]: https://www.rust-lang.org/policies/code-of-conduct
6868
[team]: https://github.com/orgs/rust-embedded-community/teams/all

src/lib.rs

Lines changed: 43 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@
99
//! that the interrupt can access, which is unsafe.
1010
1111
#![cfg_attr(not(test), no_std)]
12+
#[cfg(test)]
13+
extern crate std as core;
1214

1315
// ****************************************************************************
1416
//
1517
// Imports
1618
//
1719
// ****************************************************************************
1820

19-
#[cfg(not(test))]
2021
use core::marker::PhantomData;
2122

22-
#[cfg(test)]
23-
use std::marker::PhantomData;
24-
2523
// ****************************************************************************
2624
//
2725
// Modules
@@ -314,7 +312,7 @@ where
314312
S: ScancodeSet,
315313
{
316314
/// Make a new Keyboard object with the given layout.
317-
pub fn new(_layout: T, _set: S, handle_ctrl: HandleControl) -> Keyboard<T, S> {
315+
pub const fn new(handle_ctrl: HandleControl) -> Keyboard<T, S> {
318316
Keyboard {
319317
register: 0,
320318
num_bits: 0,
@@ -340,7 +338,7 @@ where
340338
}
341339

342340
/// Get the current Ctrl key mapping.
343-
pub fn get_ctrl_handling(&self) -> HandleControl {
341+
pub const fn get_ctrl_handling(&self) -> HandleControl {
344342
self.handle_ctrl
345343
}
346344

@@ -490,16 +488,16 @@ where
490488
}
491489
}
492490

493-
fn get_bit(word: u16, offset: usize) -> bool {
491+
const fn get_bit(word: u16, offset: usize) -> bool {
494492
((word >> offset) & 0x0001) != 0
495493
}
496494

497-
fn has_even_number_bits(data: u8) -> bool {
495+
const fn has_even_number_bits(data: u8) -> bool {
498496
(data.count_ones() % 2) == 0
499497
}
500498

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

527525
impl KeyEvent {
528-
pub fn new(code: KeyCode, state: KeyState) -> KeyEvent {
526+
pub const fn new(code: KeyCode, state: KeyState) -> KeyEvent {
529527
KeyEvent { code, state }
530528
}
531529
}
@@ -537,15 +535,15 @@ impl KeyEvent {
537535
// ****************************************************************************
538536

539537
impl Modifiers {
540-
pub fn is_shifted(&self) -> bool {
538+
pub const fn is_shifted(&self) -> bool {
541539
self.lshift | self.rshift
542540
}
543541

544-
pub fn is_ctrl(&self) -> bool {
542+
pub const fn is_ctrl(&self) -> bool {
545543
self.lctrl | self.rctrl
546544
}
547545

548-
pub fn is_caps(&self) -> bool {
546+
pub const fn is_caps(&self) -> bool {
549547
(self.lshift | self.rshift) ^ self.capslock
550548
}
551549
}
@@ -564,11 +562,8 @@ mod test {
564562

565563
#[test]
566564
fn test_f9() {
567-
let mut k = Keyboard::new(
568-
layouts::Us104Key,
569-
ScancodeSet2,
570-
HandleControl::MapLettersToUnicode,
571-
);
565+
let mut k =
566+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
572567
// start
573568
assert_eq!(k.add_bit(false), Ok(None));
574569
// 8 data bits (LSB first)
@@ -591,11 +586,8 @@ mod test {
591586

592587
#[test]
593588
fn test_f9_word() {
594-
let mut k = Keyboard::new(
595-
layouts::Us104Key,
596-
ScancodeSet2,
597-
HandleControl::MapLettersToUnicode,
598-
);
589+
let mut k =
590+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
599591
assert_eq!(
600592
k.add_word(0x0402),
601593
Ok(Some(KeyEvent::new(KeyCode::F9, KeyState::Down)))
@@ -604,11 +596,8 @@ mod test {
604596

605597
#[test]
606598
fn test_f9_byte() {
607-
let mut k = Keyboard::new(
608-
layouts::Us104Key,
609-
ScancodeSet2,
610-
HandleControl::MapLettersToUnicode,
611-
);
599+
let mut k =
600+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
612601
assert_eq!(
613602
k.add_byte(0x01),
614603
Ok(Some(KeyEvent::new(KeyCode::F9, KeyState::Down)))
@@ -617,11 +606,8 @@ mod test {
617606

618607
#[test]
619608
fn test_keyup_keydown() {
620-
let mut k = Keyboard::new(
621-
layouts::Us104Key,
622-
ScancodeSet2,
623-
HandleControl::MapLettersToUnicode,
624-
);
609+
let mut k =
610+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
625611
assert_eq!(
626612
k.add_byte(0x01),
627613
Ok(Some(KeyEvent::new(KeyCode::F9, KeyState::Down)))
@@ -639,11 +625,8 @@ mod test {
639625

640626
#[test]
641627
fn test_f5() {
642-
let mut k = Keyboard::new(
643-
layouts::Us104Key,
644-
ScancodeSet2,
645-
HandleControl::MapLettersToUnicode,
646-
);
628+
let mut k =
629+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
647630
// start
648631
assert_eq!(k.add_bit(false), Ok(None));
649632
// 8 data bits (LSB first)
@@ -666,11 +649,8 @@ mod test {
666649

667650
#[test]
668651
fn test_f5_up() {
669-
let mut k = Keyboard::new(
670-
layouts::Us104Key,
671-
ScancodeSet2,
672-
HandleControl::MapLettersToUnicode,
673-
);
652+
let mut k =
653+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
674654
// Send F0
675655

676656
// start
@@ -713,11 +693,8 @@ mod test {
713693

714694
#[test]
715695
fn test_shift() {
716-
let mut k = Keyboard::new(
717-
layouts::Uk105Key,
718-
ScancodeSet2,
719-
HandleControl::MapLettersToUnicode,
720-
);
696+
let mut k =
697+
Keyboard::<layouts::Uk105Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
721698
// A with left shift held
722699
assert_eq!(
723700
k.process_keyevent(KeyEvent::new(KeyCode::ShiftLeft, KeyState::Down)),
@@ -815,11 +792,8 @@ mod test {
815792

816793
#[test]
817794
fn test_ctrl() {
818-
let mut k = Keyboard::new(
819-
layouts::Us104Key,
820-
ScancodeSet2,
821-
HandleControl::MapLettersToUnicode,
822-
);
795+
let mut k =
796+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
823797
// Normal
824798
assert_eq!(
825799
k.process_keyevent(KeyEvent::new(KeyCode::A, KeyState::Down)),
@@ -876,11 +850,8 @@ mod test {
876850

877851
#[test]
878852
fn test_numlock() {
879-
let mut k = Keyboard::new(
880-
layouts::Uk105Key,
881-
ScancodeSet2,
882-
HandleControl::MapLettersToUnicode,
883-
);
853+
let mut k =
854+
Keyboard::<layouts::Uk105Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
884855

885856
// Numlock ON by default
886857
assert_eq!(
@@ -946,11 +917,8 @@ mod test {
946917

947918
#[test]
948919
fn test_set_1_down_up_down() {
949-
let mut k = Keyboard::new(
950-
layouts::Us104Key,
951-
ScancodeSet1,
952-
HandleControl::MapLettersToUnicode,
953-
);
920+
let mut k =
921+
Keyboard::<layouts::Us104Key, ScancodeSet1>::new(HandleControl::MapLettersToUnicode);
954922
assert_eq!(
955923
k.add_byte(0x1e),
956924
Ok(Some(KeyEvent::new(KeyCode::A, KeyState::Down)))
@@ -967,11 +935,8 @@ mod test {
967935

968936
#[test]
969937
fn test_set_1_ext_down_up_down() {
970-
let mut k = Keyboard::new(
971-
layouts::Us104Key,
972-
ScancodeSet1,
973-
HandleControl::MapLettersToUnicode,
974-
);
938+
let mut k =
939+
Keyboard::<layouts::Us104Key, ScancodeSet1>::new(HandleControl::MapLettersToUnicode);
975940
assert_eq!(k.add_byte(0xe0), Ok(None));
976941
assert_eq!(
977942
k.add_byte(0x1c),
@@ -986,11 +951,8 @@ mod test {
986951

987952
#[test]
988953
fn test_set_2_poweron() {
989-
let mut k = Keyboard::new(
990-
layouts::Us104Key,
991-
ScancodeSet2,
992-
HandleControl::MapLettersToUnicode,
993-
);
954+
let mut k =
955+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
994956
assert_eq!(
995957
k.add_byte(0xAA),
996958
Ok(Some(KeyEvent::new(
@@ -1002,11 +964,8 @@ mod test {
1002964

1003965
#[test]
1004966
fn test_set_2_toomanykeys() {
1005-
let mut k = Keyboard::new(
1006-
layouts::Us104Key,
1007-
ScancodeSet2,
1008-
HandleControl::MapLettersToUnicode,
1009-
);
967+
let mut k =
968+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
1010969
assert_eq!(
1011970
k.add_byte(0x00),
1012971
Ok(Some(KeyEvent::new(
@@ -1018,11 +977,8 @@ mod test {
1018977

1019978
#[test]
1020979
fn test_set_2_down_up() {
1021-
let mut k = Keyboard::new(
1022-
layouts::Us104Key,
1023-
ScancodeSet2,
1024-
HandleControl::MapLettersToUnicode,
1025-
);
980+
let mut k =
981+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
1026982
assert_eq!(
1027983
k.add_byte(0x29),
1028984
Ok(Some(KeyEvent::new(KeyCode::Spacebar, KeyState::Down)))
@@ -1054,11 +1010,8 @@ mod test {
10541010

10551011
#[test]
10561012
fn test_set_2_ext_down_up() {
1057-
let mut k = Keyboard::new(
1058-
layouts::Us104Key,
1059-
ScancodeSet2,
1060-
HandleControl::MapLettersToUnicode,
1061-
);
1013+
let mut k =
1014+
Keyboard::<layouts::Us104Key, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
10621015
assert_eq!(k.add_byte(0xE0), Ok(None));
10631016
assert_eq!(
10641017
k.add_byte(0x6C),
@@ -1074,11 +1027,8 @@ mod test {
10741027

10751028
#[test]
10761029
fn test_frazert() {
1077-
let mut k = Keyboard::new(
1078-
layouts::Azerty,
1079-
ScancodeSet2,
1080-
HandleControl::MapLettersToUnicode,
1081-
);
1030+
let mut k =
1031+
Keyboard::<layouts::Azerty, ScancodeSet2>::new(HandleControl::MapLettersToUnicode);
10821032
assert_eq!(
10831033
k.process_keyevent(KeyEvent::new(KeyCode::NumpadSlash, KeyState::Down)),
10841034
Some(DecodedKey::Unicode('/'))

0 commit comments

Comments
 (0)