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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| use std::fmt; use std::io::{self, Read, Write}; const MAX_MSG_LEN: usize = 0x50; struct Msg { data: [u8; MAX_MSG_LEN], } impl Msg { #[inline(never)] fn new() -> Self { Msg { data: [0; MAX_MSG_LEN], } } } impl fmt::Display for Msg { #[inline(never)] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self.data) } } #[inline(never)] fn prompt(msg: String) { print!("{} > ", msg); io::stdout().flush().unwrap(); } struct ChatBox { msg_list: Vec<&'static mut Msg>, } impl ChatBox { #[inline(never)] fn new() -> Self { ChatBox { msg_list: Vec::new(), } } #[inline(never)] fn add_msg(&mut self) { println!("Adding a new message"); self.msg_list.push(self.get_ptr()); println!( "Successfully added a new message with index: {}", self.msg_list.len() - 1 ); } #[inline(never)] fn show_msg(&mut self) { prompt("Index".parse().unwrap()); let mut index = String::new(); io::stdin().read_line(&mut index).expect("Failed to read"); let index: usize = index.trim().parse().expect("Invalid!"); println!("Content: {}", self.msg_list[index]); } #[inline(never)] fn edit_msg(&mut self) { prompt("Index".parse().unwrap()); let mut index = String::new(); io::stdin().read_line(&mut index).expect("Failed to read"); let index: usize = index.trim().parse().expect("Invalid!"); prompt("Content".parse().unwrap()); let mut handle = io::stdin().lock(); handle.read(&mut self.msg_list[index].data).expect("Failed to read"); println!("Content: {}", self.msg_list[index]); } #[inline(never)] fn delete_msg(&mut self) { prompt("Index".parse().unwrap()); let mut index = String::new(); io::stdin().read_line(&mut index).expect("Failed to read"); let index: usize = index.trim().parse().expect("Invalid!"); self.msg_list.remove(index); } #[inline(never)] fn get_ptr(&self) -> &'static mut Msg { const S: &&() = &&(); fn get_ptr<'a, 'b, T: ?Sized>(x: &'a mut T) -> &'b mut T { fn ident<'a, 'b, T: ?Sized>(_val_a: &'a &'b (), val_b: &'b mut T) -> &'a mut T { val_b } let f: fn(_, &'a mut T) -> &'b mut T = ident; f(S, x) } let mut msg = Msg::new(); get_ptr(&mut msg) } }
#[inline(never)] fn main() { let mut chat_box = ChatBox::new(); println!("I am a chatting bot of QWB S8, you can chat with me."); println!("If you delight me, I will give you flag!"); println!("This is function menu: "); println!("1. add"); println!("2. show"); println!("3. edit"); println!("4. delete"); println!("5. exit"); loop { prompt("Choice".parse().unwrap()); let mut choice = String::new(); io::stdin().read_line(&mut choice).expect("Failed to read"); let choice: i8 = choice.trim().parse().expect("Invalid!"); match choice { 1 => chat_box.add_msg(), 2 => chat_box.show_msg(), 3 => chat_box.edit_msg(), 4 => chat_box.delete_msg(), 5 => break, _ => println!("Invalid Choice!") } } }
|