sinner/
lib.rs

1use std::ops::{Deref, DerefMut};
2use std::mem::MaybeUninit;
3
4#[cfg(not(debug_assertions))]
5compile_error!("D̴o̴n̸'̵t̷ ̷d̶e̵p̸l̶o̸y̵ ̵t̸h̷i̸s̸ ̵s̶h̴i̷t̷ ̵t̶o̴ ̷p̴r̸o̴d̶u̴c̷t̵i̵o̷n̴ ̷y̴o̷u̷ ̷m̶a̴d̸m̶a̸n̶");
6
7mod list_of_the_damned;
8
9pub use list_of_the_damned::ListOfTheDamned;
10
11#[derive(Clone)]
12pub struct Sin<T> {
13    ptr: *mut Sinner<T>,
14}
15
16pub struct Sinner<T> {
17    data: T,
18    ref_count: usize,
19}
20
21unsafe impl<T> Send for Sin<T> {}
22impl<T: Clone> Copy for Sin<T>  {}
23
24impl<T> Sin<T> {
25    pub fn new(data: T) -> Self {
26        let ptr = Box::into_raw(Box::new(Sinner {
27            data,
28            ref_count: 1,
29        }));
30        Sin { ptr: ptr }
31    }
32
33    fn clone(&self) -> Self {
34        unsafe {
35            (*self.ptr).ref_count += 1;
36        }
37        Sin { ptr: self.ptr }
38    }
39}
40
41impl<T> From<T> for Sin<T> {
42    fn from(v: T) -> Self {
43        return Self::new(v);
44    }
45}
46
47impl<T> Deref for Sin<T> {
48    type Target = T;
49
50    fn deref(&self) -> &T {
51        unsafe {
52            &(*self.ptr).data
53        }
54    }
55}
56
57impl<T> DerefMut for Sin<T> where T: Clone {
58    fn deref_mut(&mut self) -> &mut T {
59        unsafe {
60            &mut (*self.ptr).data
61        }
62    }
63}
64
65pub trait UniversalSummoningCircle {
66    fn summon() -> Self;
67}
68
69impl<T> UniversalSummoningCircle for T {
70    fn summon() -> Self {
71        unsafe {
72            MaybeUninit::uninit().assume_init()
73        }
74    }
75}
76
77#[test]
78fn summon_test() {
79    let x = i32::summon();
80    let y = i32::summon();
81    let mut z = Vec::<String>::summon();
82    z.push(x.to_string());
83    z.push(y.to_string());
84}
85
86// impl<T> Drop for Sin<T> {
87//     fn drop(&mut self) {
88//         unsafe {
89//             (*self.ptr).ref_count -= 1;
90//             if (*self.ptr).ref_count == 0 {
91//                 drop(Box::from_raw(self.ptr));
92//             }
93//         }
94//     }
95// }
96
97
98// --
99
100// struct Item<T> {
101//     pub prev: Option<Self>,
102//     pub next: Option<Self>,
103//     pub value: T
104// }
105
106// struct List<T> {
107//     pub head: Option<Item<T>>,
108//     pub tail: Option<Item<T>>,
109// }
110
111// impl Default for List<u32> {
112//     fn default() -> Self {
113//         List {
114//             head: None,
115//             tail: None,
116//         }
117//     }
118// }
119
120// impl <T> List<T> where T: Clone {
121//     pub fn append(&mut self, other: T) {
122//         let mut item = Item {
123//             prev: None,
124//             next: None,
125//             value: other,
126//         };
127//         if let Some(ref mut tail) = self.tail {
128//             tail.next = Some(item);
129//             item.prev = Some(tail.clone());
130//             self.tail = Some(item);
131//         } else {
132//             self.head = Some(item);
133//             self.tail = Some(item);
134//         }
135//     }
136// }
137
138// fn main () {
139//     let mut list = List::default();
140//     list.append(1);
141//     list.append(2);
142//     list.append(3);
143
144//     let mut ptr = list.head;
145//     while let Some(item) = ptr {
146//         println!("{}", item.value);
147//         ptr = item.next;
148//     }
149// }
150
151// use std::thread::{spawn, sleep};
152
153// #[derive(Debug, Clone)]
154// struct Stuff {
155//     pub content: String,
156// }
157
158// impl Stuff {
159//     fn bang(&self) {
160//         println!("bang! {}", self.content);
161//     }
162// }
163
164// fn main() {
165//     let mut x = Stuff { content: "old".to_string() };
166//     let t = spawn({
167//         move || {
168//             sleep(std::time::Duration::from_secs(1));
169//             x.content = "new".to_string();
170//             x.bang();
171//         }
172//     });
173//     x.bang();
174//     t.join().unwrap();
175//     x.bang();
176// }