From 07a1391030bf4aed845edb75d8f1c26f7c5705be Mon Sep 17 00:00:00 2001 From: Xiretza Date: Thu, 9 Dec 2021 21:26:36 +0100 Subject: [PATCH] 2021 day8/rust: make v4 function unsafe --- 2021/day8/day8_rs/benches/unscramble.rs | 2 +- 2021/day8/day8_rs/src/v4.rs | 2 +- 2021/day8/day8_rs/tests/unscramble.rs | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/2021/day8/day8_rs/benches/unscramble.rs b/2021/day8/day8_rs/benches/unscramble.rs index fe1b3d3..335a1f3 100644 --- a/2021/day8/day8_rs/benches/unscramble.rs +++ b/2021/day8/day8_rs/benches/unscramble.rs @@ -36,7 +36,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { group.bench_function("v4", |b| { b.iter(|| { for line in &lines { - let _ = v4::unscramble(black_box(line)); + let _ = unsafe { v4::unscramble(black_box(line)) }; } }) }); diff --git a/2021/day8/day8_rs/src/v4.rs b/2021/day8/day8_rs/src/v4.rs index 76bd80a..89ec18f 100644 --- a/2021/day8/day8_rs/src/v4.rs +++ b/2021/day8/day8_rs/src/v4.rs @@ -17,7 +17,7 @@ const FREQ_TABLE: [usize; 256] = { #[must_use] #[inline] -pub fn unscramble(line: &str) -> LineResult { +pub unsafe fn unscramble(line: &str) -> LineResult { let mut bytes = line.bytes(); let mut counts = [0; 7]; diff --git a/2021/day8/day8_rs/tests/unscramble.rs b/2021/day8/day8_rs/tests/unscramble.rs index 1070a6a..5688005 100644 --- a/2021/day8/day8_rs/tests/unscramble.rs +++ b/2021/day8/day8_rs/tests/unscramble.rs @@ -73,5 +73,9 @@ pub fn test_unscramble_v3() { #[test] pub fn test_unscramble_v4() { - test_unscramble(v4::unscramble) + fn safe_v4(input: &str) -> LineResult { + unsafe { v4::unscramble(input) } + } + + test_unscramble(safe_v4) }