From e7af2cfcb4b8e57de2639a1a8ce46c33eb48e782 Mon Sep 17 00:00:00 2001 From: Finkelman Date: Wed, 15 Aug 2018 11:39:52 -0400 Subject: [PATCH 1/3] void 1.0.1 does not build on modern rust --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 2a41904..4f0d264 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ travis-ci = { repository = "Amanieu/thread_local-rs" } [dependencies] unreachable = "1.0" +void = "1.0.2" # required only for minimal-versions. brought in by unreachable. lazy_static = "1.0" From 4edbd1c0fa2cfab47de472f177c10a0bfe480006 Mon Sep 17 00:00:00 2001 From: Finkelman Date: Wed, 15 Aug 2018 13:14:04 -0400 Subject: [PATCH 2/3] inline the code from unreachable and void --- Cargo.toml | 2 -- src/lib.rs | 4 +-- src/unreachable.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/unreachable.rs diff --git a/Cargo.toml b/Cargo.toml index 4f0d264..7a84a6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,4 @@ keywords = ["thread_local", "concurrent", "thread"] travis-ci = { repository = "Amanieu/thread_local-rs" } [dependencies] -unreachable = "1.0" -void = "1.0.2" # required only for minimal-versions. brought in by unreachable. lazy_static = "1.0" diff --git a/src/lib.rs b/src/lib.rs index dde9a15..2f8e579 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,11 +70,11 @@ #![warn(missing_docs)] -extern crate unreachable; #[macro_use] extern crate lazy_static; mod thread_id; +mod unreachable; use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; use std::sync::Mutex; @@ -158,7 +158,7 @@ fn hash(id: usize, bits: usize) -> usize { #[cfg(target_pointer_width = "64")] #[inline] fn hash(id: usize, bits: usize) -> usize { - id.wrapping_mul(0x9E3779B97F4A7C15) >> (64 - bits) + id.wrapping_mul(0x9E37_79B9_7F4A_7C15) >> (64 - bits) } impl ThreadLocal { diff --git a/src/unreachable.rs b/src/unreachable.rs new file mode 100644 index 0000000..baff766 --- /dev/null +++ b/src/unreachable.rs @@ -0,0 +1,74 @@ +// Copyright 2017 Amanieu d'Antras +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +//! # unreachable +//! inlined from https://github.com/reem/rust-unreachable/ +//! +//! An unreachable code optimization hint in stable rust, and some useful +//! extension traits for `Option` and `Result`. +//! + +/// Hint to the optimizer that any code path which calls this function is +/// statically unreachable and can be removed. +/// +/// Calling this function in reachable code invokes undefined behavior. Be +/// very, very sure this is what you want; often, a simple `panic!` is more +/// suitable. +#[inline] +pub unsafe fn unreachable() -> ! { + /// The empty type for cases which can't occur. + enum Void { } + let x: &Void = ::std::mem::transmute(1usize); + match *x {} +} + +/// An extension trait for `Option` providing unchecked unwrapping methods. +pub trait UncheckedOptionExt { + /// Get the value out of this Option without checking for None. + unsafe fn unchecked_unwrap(self) -> T; + + /// Assert that this Option is a None to the optimizer. + unsafe fn unchecked_unwrap_none(self); +} + +/// An extension trait for `Result` providing unchecked unwrapping methods. +pub trait UncheckedResultExt { + /// Get the value out of this Result without checking for Err. + unsafe fn unchecked_unwrap_ok(self) -> T; + + /// Get the error out of this Result without checking for Ok. + unsafe fn unchecked_unwrap_err(self) -> E; +} + +impl UncheckedOptionExt for Option { + unsafe fn unchecked_unwrap(self) -> T { + match self { + Some(x) => x, + None => unreachable() + } + } + + unsafe fn unchecked_unwrap_none(self) { + if self.is_some() { unreachable() } + } +} + +impl UncheckedResultExt for Result { + unsafe fn unchecked_unwrap_ok(self) -> T { + match self { + Ok(x) => x, + Err(_) => unreachable() + } + } + + unsafe fn unchecked_unwrap_err(self) -> E { + match self { + Ok(_) => unreachable(), + Err(e) => e + } + } +} \ No newline at end of file From f8ca900812aeceac9f7c808bc44a3ea7a1c1212e Mon Sep 17 00:00:00 2001 From: Finkelman Date: Wed, 15 Aug 2018 13:31:13 -0400 Subject: [PATCH 3/3] update MSRV to match lazy-static --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9ae5325..1301a4a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ rust: - nightly - beta - stable -- 1.9.0 +- 1.21.0 before_script: - |