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

TimeZone should be able to be used as a trait object #432

Open
mleonhard opened this issue Jun 11, 2020 · 3 comments
Open

TimeZone should be able to be used as a trait object #432

mleonhard opened this issue Jun 11, 2020 · 3 comments

Comments

@mleonhard
Copy link

Hi chrono maintainers,
Is there any way to store a TimeZone in an Arc?

Best,
Michael

// src/main.rs
struct TimezoneHolder {
    tz: std::sync::Arc<dyn chrono::TimeZone>,
}

pub fn main() -> () {
    let _holder = TimezoneHolder { tz: std::sync::Arc::new(chrono::Utc) };
}
# Cargo.toml
[package]
name = "example"
version = "0.1.0"
edition = "2018"

[dependencies]
chrono = "0.4.11"
$ cargo run
   Compiling example v0.1.0 (/Users/user/rust-in-production/example)
error[E0191]: the value of the associated type `Offset` (from trait `chrono::offset::TimeZone`) must be specified
 --> src/main.rs:3:28
  |
3 |     tz: std::sync::Arc<dyn chrono::TimeZone>,
  |                            ^^^^^^^^^^^^^^^^ help: specify the associated type: `chrono::TimeZone<Offset = Type>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0191`.
error: could not compile `example`.

To learn more, run the command again with --verbose.
@mleonhard
Copy link
Author

The solution is to use chrono_tz::Tz enum values instead of the chrono::TimeZone trait.

# Cargo.toml
[package]
name = "example"
version = "0.1.0"
edition = "2018"

[dependencies]
chrono = "0.4.11"
chrono-tz = "0.5.2"
// src/main.rs
use std::sync::Arc;

use chrono_tz::Tz;

#[derive(Debug)]
struct TimezoneHolder {
    value: Arc<Tz>,
}

pub fn main() -> () {
    let holder = TimezoneHolder { value: Arc::new(Tz::America__Los_Angeles) };
    println!("holder is {:?}", holder);
}
$ cargo run
holder is TimezoneHolder { value: America/Los_Angeles }

@quodlibetor
Copy link
Contributor

Yeah unfortunately I believe that the inner Offset inside of TimeZone means it cannot be used as a trait object. I'd like to get rid of the Offset associated type, but I haven't figured out how to make it work yet.

@quodlibetor quodlibetor changed the title How to store TimeZone in Arc? TimeZone should be able to be used as a trait object Jun 11, 2020
@WhyNotHugo
Copy link

See also: #822

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants