From ba7077b32df42d345c9e6482665db4ae4ecbb4b3 Mon Sep 17 00:00:00 2001 From: Koichi Akabe Date: Mon, 20 Jun 2022 14:44:55 +0900 Subject: [PATCH] Use wrapping_ methods to support older version of Rust (#48) * Use wrapping_ methods to support older version of Rust * Add CI rule * Support Rust 1.58 in predict command --- .github/workflows/rust.yml | 22 +++++++++++++++++ predict/src/main.rs | 5 ++-- vaporetto/Cargo.toml | 2 +- vaporetto/src/utils.rs | 47 +++++++++++++++++------------------- vaporetto_rules/Cargo.toml | 2 +- vaporetto_tantivy/Cargo.toml | 2 +- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ffe08c10..fdf3d449 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -7,6 +7,24 @@ on: name: build jobs: + msrv: + name: MSRV + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install MSRV + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.58 + override: true + profile: minimal + + - name: Run cargo check + uses: actions-rs/cargo@v1 + with: + command: check + args: --features kytea,train + stable: name: Stable runs-on: ubuntu-latest @@ -17,12 +35,14 @@ jobs: with: toolchain: stable override: true + profile: minimal components: rustfmt, clippy - name: Run cargo check uses: actions-rs/cargo@v1 with: command: check + args: --features kytea,train - name: Run cargo fmt uses: actions-rs/cargo@v1 @@ -100,12 +120,14 @@ jobs: with: toolchain: nightly override: true + profile: minimal components: rustfmt, clippy - name: Run cargo check uses: actions-rs/cargo@v1 with: command: check + args: --all-features - name: Run cargo fmt uses: actions-rs/cargo@v1 diff --git a/predict/src/main.rs b/predict/src/main.rs index 2502177c..016ba46f 100644 --- a/predict/src/main.rs +++ b/predict/src/main.rs @@ -103,10 +103,11 @@ fn main() -> Result<(), Box> { eprintln!("Start tokenization"); let start = Instant::now(); + let stdout = io::stdout(); let mut out: Box = if args.buffered_out { - Box::new(BufWriter::new(io::stdout().lock())) + Box::new(BufWriter::new(stdout.lock())) } else { - Box::new(io::stdout().lock()) + Box::new(stdout.lock()) }; let mut buf = String::new(); let mut s = Sentence::default(); diff --git a/vaporetto/Cargo.toml b/vaporetto/Cargo.toml index 87a103fa..edd1e59a 100644 --- a/vaporetto/Cargo.toml +++ b/vaporetto/Cargo.toml @@ -2,7 +2,7 @@ name = "vaporetto" version = "0.5.0" edition = "2021" -rust-version = "1.60" +rust-version = "1.58" authors = ["Koichi Akabe "] description = "Vaporetto: a pointwise prediction based tokenizer" license = "MIT OR Apache-2.0" diff --git a/vaporetto/src/utils.rs b/vaporetto/src/utils.rs index defe8fe3..2d55b5f4 100644 --- a/vaporetto/src/utils.rs +++ b/vaporetto/src/utils.rs @@ -1,5 +1,4 @@ use core::hash::{BuildHasher, Hash, Hasher}; -use core::num::Wrapping; use core::ops::{Deref, DerefMut}; use alloc::vec::Vec; @@ -78,72 +77,70 @@ where // Copied from https://prng.di.unimi.it/splitmix64.c pub struct SplitMix64 { - x: Wrapping, + x: u64, +} + +impl SplitMix64 { + fn add(&mut self, i: u64) { + self.x ^= i; + self.x = self.x.wrapping_add(0x9e3779b97f4a7c15); + self.x = (self.x ^ (self.x >> 30)).wrapping_mul(0xbf58476d1ce4e5b9); + self.x = (self.x ^ (self.x >> 27)).wrapping_mul(0x94d049bb133111eb); + self.x = self.x ^ (self.x >> 31); + } } impl Hasher for SplitMix64 { #[inline(always)] fn finish(&self) -> u64 { - let mut z = self.x; - z = (z ^ (z >> 30)) * Wrapping(0xbf58476d1ce4e5b9); - z = (z ^ (z >> 27)) * Wrapping(0x94d049bb133111eb); - (z ^ (z >> 31)).0 + self.x } #[inline(always)] fn write(&mut self, bytes: &[u8]) { for &i in bytes { - self.x ^= u64::from(i); - self.x += 0x9e3779b97f4a7c15; + self.add(u64::from(i)); } } #[inline(always)] fn write_u8(&mut self, i: u8) { - self.x ^= u64::from(i); - self.x += 0x9e3779b97f4a7c15; + self.add(u64::from(i)); } #[inline(always)] fn write_u16(&mut self, i: u16) { - self.x ^= u64::from(i); - self.x += 0x9e3779b97f4a7c15; + self.add(u64::from(i)); } #[inline(always)] fn write_u32(&mut self, i: u32) { - self.x ^= u64::from(i); - self.x += 0x9e3779b97f4a7c15; + self.add(u64::from(i)); } #[inline(always)] fn write_u64(&mut self, i: u64) { - self.x ^= i; - self.x += 0x9e3779b97f4a7c15; + self.add(i); } #[inline(always)] fn write_i8(&mut self, i: i8) { - self.x ^= i as u64; - self.x += 0x9e3779b97f4a7c15; + self.add(i as u64); } #[inline(always)] fn write_i16(&mut self, i: i16) { - self.x ^= i as u64; - self.x += 0x9e3779b97f4a7c15; + self.add(i as u64); } #[inline(always)] fn write_i32(&mut self, i: i32) { - self.x ^= i as u64; - self.x += 0x9e3779b97f4a7c15; + self.add(i as u64); } #[inline(always)] fn write_i64(&mut self, i: i64) { - self.x ^= i as u64; - self.x += 0x9e3779b97f4a7c15; + self.add(i as u64); } } @@ -155,7 +152,7 @@ impl BuildHasher for SplitMix64Builder { #[inline(always)] fn build_hasher(&self) -> Self::Hasher { - SplitMix64 { x: Wrapping(0) } + SplitMix64 { x: 0 } } } diff --git a/vaporetto_rules/Cargo.toml b/vaporetto_rules/Cargo.toml index 835829ae..91874863 100644 --- a/vaporetto_rules/Cargo.toml +++ b/vaporetto_rules/Cargo.toml @@ -2,7 +2,7 @@ name = "vaporetto_rules" version = "0.5.0" edition = "2021" -rust-version = "1.60" +rust-version = "1.58" authors = ["Koichi Akabe "] description = "Rule-base filters for Vaporetto" license = "MIT OR Apache-2.0" diff --git a/vaporetto_tantivy/Cargo.toml b/vaporetto_tantivy/Cargo.toml index c2513132..06f119d6 100644 --- a/vaporetto_tantivy/Cargo.toml +++ b/vaporetto_tantivy/Cargo.toml @@ -2,7 +2,7 @@ name = "vaporetto_tantivy" version = "0.5.0" edition = "2021" -rust-version = "1.60" +rust-version = "1.58" authors = ["Koichi Akabe "] description = "Vaporetto Tokenizer for Tantivy" license = "MIT OR Apache-2.0"