Sha256: 7fc9e31f5cf76f5e7ff12e6abdcedc2acbde3424b22a997a0ea4c9303bb56f17

Contents?: true

Size: 1.22 KB

Versions: 16

Compression:

Stored size: 1.22 KB

Contents

use magnus::{
    rb_sys::{protect, AsRawValue},
    RString,
};

pub trait Tmplock {
    fn as_locked_slice(&self) -> Result<(&[u8], TmplockGuard), magnus::Error>;
    fn as_locked_str(&self) -> Result<(&str, TmplockGuard), magnus::Error>;
}

#[derive(Debug)]
#[repr(transparent)]
pub struct TmplockGuard {
    raw: rb_sys::VALUE,
}

impl Drop for TmplockGuard {
    fn drop(&mut self) {
        let result = unsafe { protect(|| rb_sys::rb_str_unlocktmp(self.raw)) };
        debug_assert!(
            result.is_ok(),
            "failed to unlock tmplock for unknown reason"
        );
    }
}

impl Tmplock for RString {
    fn as_locked_slice(&self) -> Result<(&[u8], TmplockGuard), magnus::Error> {
        let raw = self.as_raw();
        let slice = unsafe { self.as_slice() };
        let raw = protect(|| unsafe { rb_sys::rb_str_locktmp(raw) })?;
        let guard = TmplockGuard { raw };

        Ok((slice, guard))
    }

    fn as_locked_str(&self) -> Result<(&str, TmplockGuard), magnus::Error> {
        let str_result = unsafe { self.as_str()? };
        let raw = self.as_raw();
        let raw = protect(|| unsafe { rb_sys::rb_str_locktmp(raw) })?;
        let guard = TmplockGuard { raw };

        Ok((str_result, guard))
    }
}

Version data entries

16 entries across 16 versions & 1 rubygems

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