1use std::io::{Cursor};
2use byteorder::{BigEndian, WriteBytesExt,ReadBytesExt};
3use std::sync::Arc;
4use tokio::sync::{Mutex,Notify};
5
6
7#[allow(dead_code)]
8pub fn debug_error(e:&'static str,c:bool){
9 if c{
10 println!("!!! {}",e);
11 }
12}
13
14#[allow(dead_code)]
15pub fn debug_message(e:&'static str,c:bool){
16 if c{
17 println!(">>> {}",e);
18 }
19}
20
21#[allow(dead_code)]
22pub fn u64_from_bytes(pool:&Vec<u8>)->Result<u64,()>{
23 let mut rdr = Cursor::new(pool);
25 match rdr.read_u64::<BigEndian>(){
26 Ok(v)=>{return Ok(v)},
27 Err(_e)=>{
28 return Err(());
30 }
31 }
32}
33
34#[allow(dead_code)]
35pub fn u64_to_bytes(n:u64)->Result<Vec<u8>,()>{
36 let mut value_len_as_bytes = Vec::new();
37 match value_len_as_bytes.write_u64::<BigEndian>(n){
38 Ok(_)=>{
39 return Ok(value_len_as_bytes);
41 },
42 Err(_)=>{
43 return Err(());
44 }
45 }
46}
47
48#[derive(Clone,Debug,Copy,Default)]
49pub struct Pointer{
50 pub item_index:u64,
51 pub map_index:u8
52}
53
54#[derive(Clone,Debug)]
55pub enum SignalData{
56 None,Value((Vec<u8>,Pointer))
57}
58
59#[derive(Clone,Debug)]
60pub struct Signal{
61 pub result:bool,
62 pub waker:Arc<Notify>,
63 pub data:SignalData
64}
65
66impl Signal{
67 pub fn new()->(Arc<Mutex<Signal>>,Arc<Notify>){
68 let sleeper = Arc::new(Notify::new());
69 (
70 Arc::new(
71 Mutex::new(
72 Signal{
73 result:false,
74 waker:sleeper.clone(),
75 data:SignalData::None
76 }
77 )
78 ),
79 sleeper
80 )
81 }
82 pub async fn ok(hold:Arc<Mutex<Signal>>){
83 let mut lock = hold.lock().await;
84 lock.result = true;
85 lock.waker.notify_one();
86 }
87 pub async fn data(hold:Arc<Mutex<Signal>>,data:SignalData){
88 let mut lock = hold.lock().await;
89 lock.result = true;
90 lock.data = data;
91 lock.waker.notify_one();
92 }
93 pub async fn error(hold:Arc<Mutex<Signal>>){
94 let lock = hold.lock().await;
95 lock.waker.notify_one();
96 }
97 pub async fn check(hold:&Arc<Mutex<Signal>>)->bool{
98 let lock = hold.lock().await;
99 return lock.result;
100 }
101 pub async fn get(hold:Arc<Mutex<Signal>>)->Result<SignalData,&'static str>{
102 match Arc::try_unwrap(hold){
103 Ok(v)=>{
104 let hold = v.into_inner();
105 return Ok(hold.data);
106 },
107 Err(v)=>{
108 let lock = v.lock().await;
109 return Ok(lock.data.clone());
110 }
112 }
113 }
116}
117
118
119