Sha256: 44005f0b1b0c99c3ac491c31b15264ac801e00101b4910fd855b37a13fa7fa3e

Contents?: true

Size: 1.53 KB

Versions: 38

Compression:

Stored size: 1.53 KB

Contents

extern crate maybe_owned;

use maybe_owned::MaybeOwned;
use std::collections::HashMap;
use std::time::SystemTime;

struct Data {
    text: String,
    // this should be some think like
    // chrono::Date, but then it's just an examples
    time: SystemTime,
}

impl Data {
    fn new<T>(text: T) -> Data
    where
        T: Into<String>,
    {
        Data {
            text: text.into(),
            time: SystemTime::now(),
        }
    }
}

#[derive(Default)]
struct Regestry<'a> {
    registry: HashMap<String, MaybeOwned<'a, Data>>,
}

impl<'a> Regestry<'a> {
    fn new() -> Regestry<'a> {
        Default::default()
    }

    fn register_data<K, D>(&mut self, key: K, data: D) -> Option<MaybeOwned<'a, Data>>
    where
        K: Into<String>,
        D: Into<MaybeOwned<'a, Data>>,
    {
        self.registry.insert(key.into(), data.into())
    }

    fn print_me(&self) {
        for (key, val) in self.registry.iter() {
            println!(
                "got: {:>6} => {:>11} {:<10} @ {:10.10?}",
                //we can just deref MaybeOwned
                key,
                val.text,
                if val.is_owned() {
                    "[owned]"
                } else {
                    "[borrowed]"
                },
                val.time
            )
        }
    }
}

fn main() {
    let shared_data = Data::new("--missing--");

    let mut reg = Regestry::new();
    reg.register_data("tom", Data::new("abc"));
    reg.register_data("lucy", &shared_data);
    reg.register_data("peter", &shared_data);
    reg.print_me();
}

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-28.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-27.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-26.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-25.0.2 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-25.0.1 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-25.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-24.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-23.0.2 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-22.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-21.0.1 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-20.0.2 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-20.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-18.0.3 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-17.0.1 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-17.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-16.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-15.0.1 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-15.0.0 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs
wasmtime-14.0.4 ./ext/cargo-vendor/maybe-owned-0.3.4/examples/registry.rs