Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 24181fd
  • /
  • src
  • /
  • intern.rs
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:94f2118334e642e09e59553caad87ab283fd7d83
directory badge Iframe embedding
swh:1:dir:a911de597da6a3641f36085fa4cd396e548572c2
intern.rs
// This file is dual licensed under the terms of the Apache License, Version
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
// for complete details.

// This file is a backport of `pyo3::intern!` from pyo3 0.16.

#[macro_export]
macro_rules! intern {
    ($py: expr, $text: expr) => {{
        static INTERNED: $crate::intern::Interned = $crate::intern::Interned::new($text);
        INTERNED.get($py)
    }};
}

#[doc(hidden)]
pub struct Interned(
    &'static str,
    pyo3::once_cell::GILOnceCell<pyo3::Py<pyo3::types::PyString>>,
);

impl Interned {
    pub const fn new(value: &'static str) -> Self {
        Interned(value, pyo3::once_cell::GILOnceCell::new())
    }

    #[inline]
    pub fn get<'py>(&'py self, py: pyo3::Python<'py>) -> &'py pyo3::types::PyString {
        self.1
            .get_or_init(py, || pyo3::types::PyString::new(py, self.0).into())
            .as_ref(py)
    }
}

#[cfg(test)]
mod tests {
    use super::Interned;

    #[test]
    fn test_interned_new() {
        for s in ["abc", "123"] {
            Interned::new(s);
        }
    }
}

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top