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

Add support for rustc older than 1.61 #1

Merged
merged 1 commit into from
Aug 17, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [stable, 1.61.0]
rust: [stable, 1.61.0, 1.34.0]
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ authors = ["David Tolnay <dtolnay@gmail.com>"]
categories = ["no-std", "no-std::no-alloc"]
description = "Const TypeId and non-'static TypeId"
documentation = "https://docs.rs/typeid"
edition = "2021"
edition = "2018"
license = "MIT OR Apache-2.0"
repository = "https://github.com/dtolnay/typeid"
rust-version = "1.61"
rust-version = "1.34"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
33 changes: 33 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::env;
use std::process::Command;
use std::str;

fn main() {
println!("cargo:rerun-if-changed=build.rs");

let compiler = match rustc_minor_version() {
Some(compiler) => compiler,
None => return,
};

if compiler >= 80 {
println!("cargo:rustc-check-cfg=cfg(no_const_type_id)");
}

if compiler < 61 {
// Function pointer casting in const fn.
// https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html#more-capabilities-for-const-fn
println!("cargo:rustc-cfg=no_const_type_id");
}
}

fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
pieces.next()?.parse().ok()
}
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,22 @@
extern crate self as typeid;

use core::any::TypeId;
#[cfg(not(no_const_type_id))]
use core::cmp::Ordering;
#[cfg(not(no_const_type_id))]
use core::fmt::{self, Debug};
#[cfg(not(no_const_type_id))]
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use core::mem;

#[cfg(not(no_const_type_id))]
#[derive(Copy, Clone)]
pub struct ConstTypeId {
type_id_fn: fn() -> TypeId,
}

#[cfg(not(no_const_type_id))]
impl ConstTypeId {
#[must_use]
pub const fn of<T>() -> Self
Expand All @@ -140,41 +145,48 @@ impl ConstTypeId {
}
}

#[cfg(not(no_const_type_id))]
impl Debug for ConstTypeId {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.get(), formatter)
}
}

#[cfg(not(no_const_type_id))]
impl PartialEq for ConstTypeId {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
}
}

#[cfg(not(no_const_type_id))]
impl PartialEq<TypeId> for ConstTypeId {
fn eq(&self, other: &TypeId) -> bool {
self.get() == *other
}
}

#[cfg(not(no_const_type_id))]
impl Eq for ConstTypeId {}

#[cfg(not(no_const_type_id))]
impl PartialOrd for ConstTypeId {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(self, other))
}
}

#[cfg(not(no_const_type_id))]
impl Ord for ConstTypeId {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&self.get(), &other.get())
}
}

#[cfg(not(no_const_type_id))]
impl Hash for ConstTypeId {
fn hash<H: Hasher>(&self, state: &mut H) {
self.get().hash(state);
Expand Down
Loading