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

Borrow - experimental sketch, DO NOT MERGE #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ use self::unicode::Unicode;

mod ascii;
mod unicode;
pub mod noopt;

/// Case Insensitive wrapper of strings.
#[derive(Clone, Copy)]
Expand Down
54 changes: 54 additions & 0 deletions src/noopt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use core::hash::Hash;
use core::borrow::Borrow;
use crate::*;

#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
/// Unicode case-insensitive wrapper without optimisation
pub struct UniCaseNoOpt<S:?Sized>(S);

impl<S> UniCaseNoOpt<S> {
pub fn new(s: S) -> Self { UniCaseNoOpt(s) }
}
impl<S:?Sized> UniCaseNoOpt<S> {
pub fn from_ref<'s>(s: &'s S) -> &'s UniCaseNoOpt<S> {
let x: &'s S = s;
let y: &'s UniCaseNoOpt<S> = unsafe { core::mem::transmute(x) };
y
}
}
impl<S> From<S> for UniCaseNoOpt<S> {
fn from(s: S) -> UniCaseNoOpt<S> { UniCaseNoOpt(s) }
}

impl PartialEq for UniCaseNoOpt<str>
{
fn eq(&self, other: &Self) -> bool {
Unicode(&self.0) ==
Unicode(&other.0)
}
}
impl Eq for UniCaseNoOpt<str> { }

impl<S:?Sized> Hash for UniCaseNoOpt<S>
where S: AsRef<str>, for <'u> UniCase<&'u S>: Hash
{
fn hash<H: Hasher>(&self, hasher: &mut H) {
Unicode(&self.0).hash(hasher)
}
}

impl<S:AsRef<str>> Borrow<UniCaseNoOpt<str>> for UniCase<S> {
fn borrow(&self) -> &UniCaseNoOpt<str> {
UniCaseNoOpt::from_ref(self.as_ref())
}
}

#[test]
fn hashset() {
use std::collections::HashSet;

let mut hm: HashSet<UniCase<String>> = Default::default();
hm.insert(UniCase::new("ascii".to_string()));
assert!(hm.contains(UniCaseNoOpt::from_ref("Ascii")));
}
4 changes: 4 additions & 0 deletions src/unicode/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ pub fn lookup(orig: char) -> Fold {
}

#[test]
#[cfg(not(miri))]
fn lookup_consistency() {
fn lookup_naive(orig: char) -> Fold {
let single_char = match orig as u32 {
Expand Down Expand Up @@ -1993,3 +1994,6 @@ fn lookup_consistency() {
}
}
}