1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Certain bits of environmental state are too annoying to thread
//! around everywhere, so pack them into TLS.

use crate::file_text::FileText;
use crate::session::Session;
use std::cell::RefCell;
use std::rc::Rc;

pub struct Tls {
    _dummy: (),
}

#[derive(Clone)]
struct TlsFields {
    session: Rc<Session>,
    file_text: Rc<FileText>,
}

thread_local! {
    static THE_TLS_FIELDS: RefCell<Option<TlsFields>> = const { RefCell::new(None) };
}

impl Tls {
    #[cfg(test)]
    pub fn test() -> Tls {
        Self::install(Rc::new(Session::test()), Rc::new(FileText::test()))
    }

    #[cfg(test)]
    pub fn test_string(text: &str) -> Tls {
        use std::path::PathBuf;
        Self::install(
            Rc::new(Session::test()),
            Rc::new(FileText::new(PathBuf::from("tmp.txt"), String::from(text))),
        )
    }

    /// Installs `Tls` and returns a placeholder value.  When this
    /// value is dropped, the `Tls` entries will be removed. To access
    /// the values from `Tls`, call `Tls::session()` or
    /// `Tls::file_text()`.
    pub fn install(session: Rc<Session>, file_text: Rc<FileText>) -> Tls {
        let fields = TlsFields { session, file_text };

        THE_TLS_FIELDS.with(|s| {
            let mut s = s.borrow_mut();
            assert!(s.is_none());
            *s = Some(fields);
        });

        Tls { _dummy: () }
    }

    fn fields() -> TlsFields {
        THE_TLS_FIELDS.with(|s| s.borrow().clone().expect("TLS is not installed"))
    }

    pub fn session() -> Rc<Session> {
        Self::fields().session
    }

    pub fn file_text() -> Rc<FileText> {
        Self::fields().file_text
    }
}

impl Drop for Tls {
    fn drop(&mut self) {
        THE_TLS_FIELDS.with(|s| {
            *s.borrow_mut() = None;
        })
    }
}