crier

RSS and Atom feed aggregator
Info | Log | Files | Refs | README

mem.rs (1060B)


      1 use std::collections::HashMap;
      2 use std::io::Write;
      3 use std::io::Error;
      4 
      5 use crate::cache::Cache;
      6 
      7 
      8 pub struct CacheWriter {
      9     data: Vec<u8>,
     10 }
     11 
     12 pub struct MemCache {
     13     files: HashMap<String, CacheWriter>,
     14 }
     15 
     16 impl Write for CacheWriter {
     17     fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
     18         self.data.extend_from_slice(buf);
     19         Ok(self.data.len())
     20     }
     21 
     22     fn flush(&mut self) -> Result<(), Error> {
     23         Ok(())
     24     }
     25 }
     26 
     27 
     28 impl CacheWriter {
     29     pub fn new() -> CacheWriter {
     30         CacheWriter{
     31             data: Vec::new(),
     32         }
     33     }
     34 }
     35 
     36 impl MemCache {
     37     pub fn new() -> MemCache {
     38         MemCache{
     39             files: HashMap::new(),
     40         }
     41     }
     42 }
     43 
     44 impl Cache for MemCache {
     45     fn open(&mut self, id: String) -> &mut dyn Write {
     46         let w: CacheWriter;
     47         if !self.files.contains_key(&id) {
     48             w = CacheWriter::new();
     49             self.files.insert(id.clone(), w);
     50         }
     51         self.files.get_mut(&id).unwrap()
     52     }
     53 
     54     fn close(&mut self, _id: String) -> usize {
     55         return 0;
     56     }
     57 }