Sha256: 3804e4abae30ba584e2ba5a4be834ae102a56bf4a6d66cc2a7b0efd6e7d9fccd

Contents?: true

Size: 1.74 KB

Versions: 31

Compression:

Stored size: 1.74 KB

Contents

use std::ops::Deref;

use serde::{Serialize, Serializer};

use crate::fast_hash_map::FastHashMap;

#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct StringIndex(u32);

#[derive(Debug, Clone, Default)]
pub struct StringTable {
    strings: Vec<String>,
    index: FastHashMap<String, StringIndex>,
}

impl StringTable {
    pub fn index_for_string(&mut self, s: &str) -> StringIndex {
        match self.index.get(s) {
            Some(string_index) => *string_index,
            None => {
                let string_index = StringIndex(self.strings.len() as u32);
                self.strings.push(s.to_string());
                self.index.insert(s.to_string(), string_index);
                string_index
            }
        }
    }

    pub fn get_string(&self, index: StringIndex) -> Option<&str> {
        self.strings.get(index.0 as usize).map(Deref::deref)
    }
}

impl Serialize for StringTable {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.strings.serialize(serializer)
    }
}

#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct GlobalStringIndex(StringIndex);

#[derive(Debug, Clone, Default)]
pub struct GlobalStringTable {
    table: StringTable,
}

impl GlobalStringTable {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn index_for_string(&mut self, s: &str) -> GlobalStringIndex {
        GlobalStringIndex(self.table.index_for_string(s))
    }

    pub fn get_string(&self, index: GlobalStringIndex) -> Option<&str> {
        self.table.get_string(index.0)
    }
}

impl Serialize for StringIndex {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u32(self.0)
    }
}

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-28.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-27.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-26.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-25.0.2 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-25.0.1 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-25.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-24.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-23.0.2 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-22.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-21.0.1 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-20.0.2 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-20.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-18.0.3 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-17.0.1 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-17.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-16.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-15.0.1 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-15.0.0 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs
wasmtime-14.0.4 ./ext/cargo-vendor/fxprof-processed-profile-0.6.0/src/string_table.rs