totally_sync_variable/
lib.rs

1//! # the totally sync variables.
2//! ## features
3//! - make any variable, pass it round!
4//! - scoping? who needs it!
5//! - only one mutable reference? pshaw!
6//! - any numer of mutable references are allowed.
7//
8//! ## data races?
9//! dont know what your talking about.
10//!
11//! its *fearless concurrency* remember? and theres no unsafe code!
12//!
13//! besides, miri says its fiine.
14#![deny(unsafe_code)]
15#![allow(dead_code)]
16use serde::{de::DeserializeOwned, Serialize};
17use std::fs;
18use std::io::{BufReader, BufWriter};
19use std::marker::PhantomData;
20use std::ops::*;
21use std::path::Path;
22
23/// a variable stored on the filesystem (very secure 🔐)
24pub struct Var<T>(&'static str, PhantomData<T>);
25
26impl<T> Var<T> {
27    /// create a var
28    pub fn new(name: &'static str) -> Self {
29        Self(name, PhantomData::default())
30    }
31}
32
33impl<T: Serialize> Var<T> {
34    /// interior mutability!
35    /// ```
36    /// # use totally_sync_variable::*;
37    /// let x = def!(x = 5u8);
38    /// x.set(&3u8);
39    /// ```
40    pub fn set(&self, v: &T) {
41        let f = BufWriter::new(
42            fs::File::create(Path::new("/tmp/").join(self.0)).expect("become linux!"),
43        );
44        serde_json::to_writer(f, v).unwrap();
45    }
46}
47
48impl<T: DeserializeOwned> Var<T> {
49    /// procure the stored variable
50    /// ```
51    /// # use totally_sync_variable::*;
52    /// let y = def!(y = 5u8);
53    /// assert_eq!(y.get(), 5u8);
54    /// ```
55    pub fn get(&self) -> T {
56        let f = BufReader::new(fs::File::open(Path::new("/tmp/").join(self.0)).expect("oh no"));
57        serde_json::from_reader(f).unwrap()
58    }
59}
60
61macro_rules! op {
62    ($op: ident, $char: tt) => {paste::paste! {
63        impl<T: [<$op>]<T, Output = T> + Clone + DeserializeOwned + Serialize> [<$op Assign>] for Var<T> {
64            fn [<$op:lower _assign>](&mut self, rhs: Self) {
65                self.set(&(self.get().clone() $char rhs.get().clone()));
66            }
67        }
68
69        impl<T: [<$op>]<T, Output = T> + Clone + DeserializeOwned + Serialize> [<$op Assign>]<T> for Var<T> {
70            fn [<$op:lower _assign>](&mut self, rhs: T) {
71                self.set(&(self.get().clone() $char rhs));
72            }
73        }
74    }};
75}
76
77op!(Add, +);
78op!(Sub, -);
79op!(Mul, *);
80op!(Div, /);
81op!(BitAnd, &);
82op!(BitOr, |);
83op!(BitXor, ^);
84op!(Shl, <<);
85op!(Shr, >>);
86op!(Rem, %);
87
88#[macro_export]
89/// lil macro to define variables.
90/// ```
91/// # use totally_sync_variable::*;
92/// let v = def!(example = String::from("def!()"));
93/// ```
94macro_rules! def {
95    ($name:ident = $value:expr) => {{
96        let v = $crate::Var::new(stringify!($name));
97        v.set(&$value);
98        v
99    }};
100}
101
102#[macro_export]
103/// macro to reference variables from anywhere.
104/// ```
105/// # use totally_sync_variable::*;
106/// {
107///   let x = def!(cant_touch_this = 0u128);
108/// }
109/// let it_exists: u128 = refer!(cant_touch_this);
110/// assert_eq!(it_exists, 0); // yes i can!
111/// ```
112macro_rules! refer {
113    ($name:ident) => {
114        $crate::Var::new(stringify!($name)).get()
115    };
116}
117
118#[test]
119fn it_works() {
120    {
121        let mut x = def!(x = 4u8);
122        x += 4;
123        assert_eq!(x.get(), 8);
124    }
125    let v: u8 = refer!(x);
126    assert_eq!(v, 8)
127}