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

Re-write ResourceKey #1511

Merged
merged 19 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 4 additions & 0 deletions experimental/tinystr_neo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ serde = { version = "1.0.123", optional = true, default-features = false, featur
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
bincode = "1.3"
postcard = { version = "0.7", features = ["use-std"] }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was from #1514 which is now merged.

[[test]]
name = "serde"
required-features = ["serde"]
5 changes: 1 addition & 4 deletions provider/cldr/src/transform/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ impl TryFrom<&dyn CldrPaths> for NumbersProvider {

impl KeyedDataProvider for NumbersProvider {
fn supports_key(resc_key: &ResourceKey) -> Result<(), DataError> {
if resc_key.category != ResourceCategory::Decimal || resc_key.version != 1 {
return Err(DataErrorKind::MissingResourceKey.with_key(*resc_key));
}
Ok(())
resc_key.match_key(key::SYMBOLS_V1)
}
}

Expand Down
8 changes: 5 additions & 3 deletions provider/cldr/src/transform/plurals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ impl TryFrom<&dyn CldrPaths> for PluralsProvider {

impl KeyedDataProvider for PluralsProvider {
fn supports_key(resc_key: &ResourceKey) -> Result<(), DataError> {
if resc_key.category != ResourceCategory::Plurals || resc_key.version != 1 {
return Err(DataErrorKind::MissingResourceKey.with_key(*resc_key));
// TODO(#442): Clean up KeyedDataProvider
match *resc_key {
key::CARDINAL_V1 => Ok(()),
key::ORDINAL_V1 => Ok(()),
_ => Err(DataErrorKind::MissingResourceKey.with_key(*resc_key)),
}
Ok(())
}
}

Expand Down
12 changes: 9 additions & 3 deletions provider/cldr/src/transform/time_zones/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ impl TryFrom<&str> for TimeZonesProvider {

impl KeyedDataProvider for TimeZonesProvider {
fn supports_key(resc_key: &ResourceKey) -> Result<(), DataError> {
if resc_key.category != ResourceCategory::TimeZone || resc_key.version != 1 {
return Err(DataErrorKind::MissingResourceKey.with_key(*resc_key));
// TODO(#442): Clean up KeyedDataProvider
match *resc_key {
key::TIMEZONE_FORMATS_V1 => Ok(()),
key::TIMEZONE_EXEMPLAR_CITIES_V1 => Ok(()),
key::TIMEZONE_GENERIC_NAMES_LONG_V1 => Ok(()),
key::TIMEZONE_GENERIC_NAMES_SHORT_V1 => Ok(()),
key::TIMEZONE_SPECIFIC_NAMES_LONG_V1 => Ok(()),
key::TIMEZONE_SPECIFIC_NAMES_SHORT_V1 => Ok(()),
_ => Err(DataErrorKind::MissingResourceKey.with_key(*resc_key)),
}
Ok(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion provider/core/src/data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DataRequest {
/// ```
/// use icu_provider::prelude::*;
///
/// const FOO_BAR: ResourceKey = icu_provider::resource_key!(x, "foo", "bar", 1);
/// const FOO_BAR: ResourceKey = icu_provider::resource_key!("foo/bar@1");
///
/// let req_no_langid = DataRequest {
/// resource_path: ResourcePath {
Expand Down
2 changes: 1 addition & 1 deletion provider/core/src/data_provider/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::yoke;
// JSON string. It also exercises most of the data provider code paths.

/// Key for HelloAlt, used for testing mismatched types
const HELLO_ALT_KEY: ResourceKey = crate::resource_key!(Core, "helloalt", 1);
const HELLO_ALT_KEY: ResourceKey = crate::resource_key!("core/helloalt@1");

/// A data struct serialization-compatible with HelloWorldV1 used for testing mismatched types
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Yokeable, ZeroCopyFrom)]
Expand Down
2 changes: 1 addition & 1 deletion provider/core/src/dynutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
/// use icu_provider::prelude::*;
/// use icu_provider::marker::CowStrMarker;
/// use std::borrow::Cow;
/// const DEMO_KEY: ResourceKey = icu_provider::resource_key!(x, "foo", "bar", 1);
/// const DEMO_KEY: ResourceKey = icu_provider::resource_key!("foo/bar@1");
///
/// // A small DataProvider that returns owned strings
/// struct MyProvider(pub String);
Expand Down
3 changes: 1 addition & 2 deletions provider/core/src/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ use litemap::LiteMap;

pub mod key {
use crate::resource::ResourceKey;
use crate::resource_key;
pub const HELLO_WORLD_V1: ResourceKey = resource_key!(Core, "helloworld", 1);
pub const HELLO_WORLD_V1: ResourceKey = crate::resource_key!("core/helloworld@1");
}

/// A struct containing "Hello World" in the requested language.
Expand Down
67 changes: 67 additions & 0 deletions provider/core/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,70 @@ fn test_escape_for_json() {
escape_for_json("ab\u{001F}c", &mut String::new())
);
}

/// Const function to compute the FxHash of a byte array with little-endian byte order.
#[allow(dead_code)]
pub const fn fxhash_32(bytes: &[u8]) -> u32 {
// This code is adapted from https://github.com/rust-lang/rustc-hash,
// whose license text is reproduced below.
//
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[inline]
const fn hash_word_32(mut hash: u32, word: u32) -> u32 {
const ROTATE: u32 = 5;
const SEED32: u32 = 0x9e_37_79_b9;
hash = hash.rotate_left(ROTATE);
hash ^= word;
hash = hash.wrapping_mul(SEED32);
hash
}

let mut cursor = 0;
let mut hash = 0;

while bytes.len() - cursor >= 4 {
let word = u32::from_le_bytes([
bytes[cursor],
bytes[cursor + 1],
bytes[cursor + 2],
bytes[cursor + 3],
]);
hash = hash_word_32(hash, word);
cursor += 4;
}

if bytes.len() - cursor >= 2 {
let word = u16::from_le_bytes([bytes[cursor], bytes[cursor + 1]]);
hash = hash_word_32(hash, word as u32);
cursor += 2;
}

if bytes.len() - cursor >= 1 {
hash = hash_word_32(hash, bytes[cursor] as u32);
}

hash
}

#[test]
fn test_hash_word_32() {
assert_eq!(0, fxhash_32(b""));
assert_eq!(0xF3051F19, fxhash_32(b"a"));
assert_eq!(0x2F9DF119, fxhash_32(b"ab"));
assert_eq!(0xCB1D9396, fxhash_32(b"abc"));
assert_eq!(0x8628F119, fxhash_32(b"abcd"));
assert_eq!(0xBEBDB56D, fxhash_32(b"abcde"));
assert_eq!(0x1CE8476D, fxhash_32(b"abcdef"));
assert_eq!(0xC0F176A4, fxhash_32(b"abcdefg"));
assert_eq!(0x09AB476D, fxhash_32(b"abcdefgh"));
assert_eq!(0xB72F5D88, fxhash_32(b"abcdefghi"));
}
9 changes: 0 additions & 9 deletions provider/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ pub mod prelude {
pub use crate::error::DataError;
pub use crate::error::DataErrorKind;
pub use crate::marker::DataMarker;
pub use crate::resource::ResourceCategory;
pub use crate::resource::ResourceKey;
pub use crate::resource::ResourceOptions;
pub use crate::resource::ResourcePath;
Expand All @@ -164,11 +163,3 @@ pub use yoke;

// Also include the same symbols at the top level for selective inclusion
pub use prelude::*;

pub mod internal {
//! Macro dependencies; not intended to be used directly.
/// Re-export tinystr16 for macro resource_key!()
pub use tinystr::tinystr16;
/// Re-export tinystr4 for macro resource_key!()
pub use tinystr::tinystr4;
}
Loading