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

Change TimeZoneIdMapper::new() to return the borrowed variant #5441

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
34 changes: 22 additions & 12 deletions components/timezone/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ use crate::{
/// use tinystr::tinystr;
///
/// let mapper = TimeZoneIdMapper::new();
/// let mapper = mapper.as_borrowed();
///
/// // The IANA zone "Australia/Melbourne" is the BCP-47 zone "aumel":
/// assert_eq!(
Expand Down Expand Up @@ -95,24 +94,26 @@ pub struct TimeZoneIdMapper {
#[cfg(feature = "compiled_data")]
impl Default for TimeZoneIdMapper {
fn default() -> Self {
Self::new()
Self {
data: DataPayload::from_static_ref(
crate::provider::Baked::SINGLETON_IANA_TO_BCP47_MAP_V2_MARKER,
),
}
}
}

impl TimeZoneIdMapper {
/// Creates a new [`TimeZoneIdMapper`] using compiled data.
/// Creates a new [`TimeZoneIdMapperBorrowed`] using compiled data.
///
/// See [`TimeZoneIdMapper`] for an example.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
#[cfg(feature = "compiled_data")]
pub fn new() -> Self {
Self {
data: DataPayload::from_static_ref(
crate::provider::Baked::SINGLETON_IANA_TO_BCP47_MAP_V2_MARKER,
),
pub fn new() -> TimeZoneIdMapperBorrowed<'static> {
TimeZoneIdMapperBorrowed {
data: crate::provider::Baked::SINGLETON_IANA_TO_BCP47_MAP_V2_MARKER,
}
}

Expand All @@ -138,6 +139,19 @@ impl TimeZoneIdMapper {
/// Returns a borrowed version of the mapper that can be queried.
///
/// This avoids a small potential indirection cost when querying the mapper.
///
/// # Examples
///
/// ```
/// use icu::timezone::TimeZoneIdMapper;
///
/// let provider = // ...
/// # icu::timezone::provider::Baked;
/// let mapper = TimeZoneIdMapper::try_new_unstable(&provider).unwrap();
/// let mapper = mapper.as_borrowed();
///
/// println!("{:?}", mapper.iana_to_bcp47("Australia/Melbourne"));
/// ```
pub fn as_borrowed(&self) -> TimeZoneIdMapperBorrowed {
TimeZoneIdMapperBorrowed {
data: self.data.get(),
Expand Down Expand Up @@ -172,7 +186,6 @@ impl<'a> TimeZoneIdMapperBorrowed<'a> {
/// use icu_timezone::TimeZoneIdMapper;
///
/// let mapper = TimeZoneIdMapper::new();
/// let mapper = mapper.as_borrowed();
///
/// let result = mapper.iana_to_bcp47("Asia/CALCUTTA").unwrap();
///
Expand Down Expand Up @@ -206,7 +219,6 @@ impl<'a> TimeZoneIdMapperBorrowed<'a> {
/// use std::borrow::Cow;
///
/// let mapper = TimeZoneIdMapper::new();
/// let mapper = mapper.as_borrowed();
///
/// let result = mapper.normalize_iana("Asia/CALCUTTA").unwrap();
///
Expand Down Expand Up @@ -245,7 +257,6 @@ impl<'a> TimeZoneIdMapperBorrowed<'a> {
/// use std::borrow::Cow;
///
/// let mapper = TimeZoneIdMapper::new();
/// let mapper = mapper.as_borrowed();
///
/// let result = mapper.canonicalize_iana("Asia/CALCUTTA").unwrap();
///
Expand Down Expand Up @@ -310,7 +321,6 @@ impl<'a> TimeZoneIdMapperBorrowed<'a> {
/// use tinystr::tinystr;
///
/// let mapper = TimeZoneIdMapper::new();
/// let mapper = mapper.as_borrowed();
///
/// let bcp47_id = TimeZoneBcp47Id(tinystr!(8, "inccu"));
/// let result = mapper.find_canonical_iana_from_bcp47(bcp47_id).unwrap();
Expand Down
1 change: 0 additions & 1 deletion components/timezone/src/ixdtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ impl CustomTimeZone {
TimeZoneRecord::Name(iana_identifier) => {
let mapper = TimeZoneIdMapper::new();
let bcp47_id = mapper
.as_borrowed()
.iana_bytes_to_bcp47(iana_identifier)
.ok_or(ParseError::InvalidIanaIdentifier)?;

Expand Down
2 changes: 1 addition & 1 deletion components/timezone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
//! time_zone.gmt_offset = "-0600".parse::<GmtOffset>().ok();
//! let mapper = TimeZoneIdMapper::new();
//! time_zone.time_zone_id =
//! mapper.as_borrowed().iana_to_bcp47("America/Chicago");
//! mapper.iana_to_bcp47("America/Chicago");
//!
//! // Alternatively, set it directly from the BCP-47 ID
//! assert_eq!(time_zone.time_zone_id, Some(TimeZoneBcp47Id(tinystr!(8, "uschi"))));
Expand Down
Loading