Skip to content

matklad/sync_lazy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Crates.io API reference

Overview

A thread safe lazy values for Rust.

#[macro_use]
extern crate sync_lazy;

use std::collections::HashMap;
use sync_lazy::Lazy;

static GLOBAL: Lazy<HashMap<i32, String>> = sync_lazy! {
    println!("initializing global");
    let mut m = HashMap::new();
    m.insert(13, "Spica".to_string());
    m.insert(74, "Hoyten".to_string());
    m
};

fn main() {
    println!("ready");
    let xs = vec![1, 2, 3];
    let local = Lazy::new(move || {
        println!("initializing local");
        xs.into_iter().sum::<i32>()
    });

    ::std::thread::spawn(|| {
        println!("{:?}", GLOBAL.get(&13));
    }).join().unwrap();
    println!("{:?}", GLOBAL.get(&74));
    println!("{}", Lazy::force(&local));
    println!("{}", Lazy::force(&local));

    // Prints:
    //   ready
    //   initializing global
    //   Some("Spica")
    //   Some("Hoyten")
    //   initializing local
    //   6
    //   6
}

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages