Sha256: d23c5cd92b5df42511edf325d88ab44231a65ab0d250cdf158f24f618ffd9f07

Contents?: true

Size: 1.55 KB

Versions: 32

Compression:

Stored size: 1.55 KB

Contents

use magnus::rb_sys::{AsRawId, FromRawId};
use magnus::{
    value::{Id, IntoId},
    Ruby, Symbol,
};
use std::convert::TryInto;
use std::num::NonZeroUsize;

/// A static `Id` that can be used to refer to a Ruby ID.
///
/// Use `define_rb_intern!` to define it so that it will be cached in a global variable.
///
/// Magnus' `Id` can't be used for this purpose since it is not `Sync`, so cannot
/// be used as a global variable with `lazy_static` in `define_rb_intern!`.
/// See [this commit on the Magnus repo][commit].
///
/// [commit]: https://github.com/matsadler/magnus/commit/1a1c1ee874e15b0b222f7aae68bb9b5360072e57
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct StaticId(NonZeroUsize);

impl StaticId {
    // Use `define_rb_intern!` instead, which uses this function.
    pub fn intern_str(id: &'static str) -> Self {
        let id: Id = magnus::StaticSymbol::new(id).into();

        // SAFETY: Ruby will never return a `0` ID.
        StaticId(unsafe { NonZeroUsize::new_unchecked(id.as_raw() as _) })
    }
}

impl IntoId for StaticId {
    fn into_id_with(self, _: &Ruby) -> Id {
        // SAFEFY: This is safe because we know that the `Id` is something
        // returned from ruby.
        unsafe { Id::from_raw(self.0.get().try_into().expect("ID to be a usize")) }
    }
}

impl From<StaticId> for Symbol {
    fn from(static_id: StaticId) -> Self {
        let id: Id = static_id.into_id();
        id.into()
    }
}

impl std::cmp::PartialEq<Id> for StaticId {
    fn eq(&self, other: &Id) -> bool {
        other.as_raw() as usize == self.0.get()
    }
}

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ext/src/helpers/static_id.rs
wasmtime-28.0.0 ext/src/helpers/static_id.rs
wasmtime-27.0.0 ext/src/helpers/static_id.rs
wasmtime-26.0.0 ext/src/helpers/static_id.rs
wasmtime-25.0.2 ext/src/helpers/static_id.rs
wasmtime-25.0.1 ext/src/helpers/static_id.rs
wasmtime-25.0.0 ext/src/helpers/static_id.rs
wasmtime-24.0.0 ext/src/helpers/static_id.rs
wasmtime-23.0.2 ext/src/helpers/static_id.rs
wasmtime-22.0.0 ext/src/helpers/static_id.rs
wasmtime-21.0.1 ext/src/helpers/static_id.rs
wasmtime-20.0.2 ext/src/helpers/static_id.rs
wasmtime-20.0.0 ext/src/helpers/static_id.rs
wasmtime-18.0.3 ext/src/helpers/static_id.rs
wasmtime-17.0.1 ext/src/helpers/static_id.rs
wasmtime-17.0.0 ext/src/helpers/static_id.rs
wasmtime-16.0.0 ext/src/helpers/static_id.rs
wasmtime-15.0.1 ext/src/helpers/static_id.rs
wasmtime-15.0.0 ext/src/helpers/static_id.rs
wasmtime-14.0.4 ext/src/helpers/static_id.rs