<commit_before>use std::fmt::{self, Debug};

#[cfg(syn_can_use_thread_id)]
mod imp {
    use std::thread::{self, ThreadId};

    /// ThreadBound is a Sync-maker and Send-maker that allows accessing a value
    /// of type T only from the original thread on which the ThreadBound was
    /// constructed.
    pub struct ThreadBound<T> {
        value: T,
        thread_id: ThreadId,
    }

    unsafe impl<T> Sync for ThreadBound<T> {}

    // Send bound requires Copy, as otherwise Drop could run in the wrong place.
    unsafe impl<T: Copy> Send for ThreadBound<T> {}

    impl<T> ThreadBound<T> {
        pub fn new(value: T) -> Self {
            ThreadBound {
                value: value,
                thread_id: thread::current().id(),
            }
        }

        pub fn get(&self) -> Option<&T> {
            if thread::current().id() == self.thread_id {
                Some(&self.value)
            } else {
                None
            }
        }
    }
}

#[cfg(not(syn_can_use_thread_id))]
mod imp {
    pub struct ThreadBound<T> {
        value: T,
    }

    impl<T> ThreadBound<T> {
        pub fn new(value: T) -> Self {
            ThreadBound {
                value: value,
            }
        }

        pub fn get(&self) -> Option<&T> {
            Some(&self.value)
        }
    }
}

pub use self::imp::ThreadBound;

impl<T: Debug> Debug for ThreadBound<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match self.get() {
            Some(value) => Debug::fmt(value, formatter),
            None => formatter.write_str("unknown"),
        }
    }
}<commit_msg>Emulate ThreadId on old compilers<commit_after>use std::fmt::{self, Debug};

use self::thread_id::ThreadId;

/// ThreadBound is a Sync-maker and Send-maker that allows accessing a value
/// of type T only from the original thread on which the ThreadBound was
/// constructed.
pub struct ThreadBound<T> {
    value: T,
    thread_id: ThreadId,
}

unsafe impl<T> Sync for ThreadBound<T> {}

// Send bound requires Copy, as otherwise Drop could run in the wrong place.
unsafe impl<T: Copy> Send for ThreadBound<T> {}

impl<T> ThreadBound<T> {
    pub fn new(value: T) -> Self {
        ThreadBound {
            value: value,
            thread_id: thread_id::current(),
        }
    }

    pub fn get(&self) -> Option<&T> {
        if thread_id::current() == self.thread_id {
            Some(&self.value)
        } else {
            None
        }
    }
}

impl<T: Debug> Debug for ThreadBound<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match self.get() {
            Some(value) => Debug::fmt(value, formatter),
            None => formatter.write_str("unknown"),
        }
    }
}

#[cfg(syn_can_use_thread_id)]
mod thread_id {
    use std::thread;

    pub use std::thread::ThreadId;

    pub fn current() -> ThreadId {
        thread::current().id()
    }
}

#[cfg(not(syn_can_use_thread_id))]
mod thread_id {
    use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};

    thread_local! {
        static THREAD_ID: usize = {
            static NEXT_THREAD_ID: AtomicUsize = ATOMIC_USIZE_INIT;
            NEXT_THREAD_ID.fetch_add(1, Ordering::Relaxed)
        };
    }

    pub type ThreadId = usize;

    pub fn current() -> ThreadId {
        THREAD_ID.with(|id| *id)
    }
}
{'fraction_non_alphanumeric': 0.12972329663790538, 'fraction_numerical': 0.0002975304968759298, 'mean_word_length': 2.1158480074142725, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6148558', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1057, 'n_words': 276}