Skip to content
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

Use wrapping_ methods to support older version of Rust #48

Merged
merged 3 commits into from
Jun 20, 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
22 changes: 22 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions predict/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

eprintln!("Start tokenization");
let start = Instant::now();
let stdout = io::stdout();
let mut out: Box<dyn Write> = 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();
Expand Down
2 changes: 1 addition & 1 deletion vaporetto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "vaporetto"
version = "0.5.0"
edition = "2021"
rust-version = "1.60"
rust-version = "1.58"
authors = ["Koichi Akabe <[email protected]>"]
description = "Vaporetto: a pointwise prediction based tokenizer"
license = "MIT OR Apache-2.0"
Expand Down
47 changes: 22 additions & 25 deletions vaporetto/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::hash::{BuildHasher, Hash, Hasher};
use core::num::Wrapping;
use core::ops::{Deref, DerefMut};

use alloc::vec::Vec;
Expand Down Expand Up @@ -78,72 +77,70 @@ where

// Copied from https://prng.di.unimi.it/splitmix64.c
pub struct SplitMix64 {
x: Wrapping<u64>,
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);
}
}

Expand All @@ -155,7 +152,7 @@ impl BuildHasher for SplitMix64Builder {

#[inline(always)]
fn build_hasher(&self) -> Self::Hasher {
SplitMix64 { x: Wrapping(0) }
SplitMix64 { x: 0 }
}
}

Expand Down
2 changes: 1 addition & 1 deletion vaporetto_rules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "vaporetto_rules"
version = "0.5.0"
edition = "2021"
rust-version = "1.60"
rust-version = "1.58"
authors = ["Koichi Akabe <[email protected]>"]
description = "Rule-base filters for Vaporetto"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion vaporetto_tantivy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "vaporetto_tantivy"
version = "0.5.0"
edition = "2021"
rust-version = "1.60"
rust-version = "1.58"
authors = ["Koichi Akabe <[email protected]>"]
description = "Vaporetto Tokenizer for Tantivy"
license = "MIT OR Apache-2.0"
Expand Down