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
use std::ops::Deref;

// Content is thread safe and owns the data
pub trait Content: ToString + Deref<Target = str> + Send + Sync + 'static {
  fn as_mut_vec(&mut self) -> &mut Vec<u8>;
}

pub enum Source {
  Plain(String),
  Customized(Box<dyn Content>),
}

use Source::*;

impl From<&str> for Source {
  fn from(s: &str) -> Self {
    Plain(s.into())
  }
}

impl Clone for Source {
  fn clone(&self) -> Self {
    match self {
      Plain(s) => Plain(s.clone()),
      Customized(_) => todo!(),
    }
  }
}

impl Deref for Source {
  type Target = str;
  fn deref(&self) -> &Self::Target {
    match self {
      Plain(s) => s.deref(),
      Customized(c) => c.deref(),
    }
  }
}

impl ToString for Source {
  fn to_string(&self) -> String {
    match self {
      Self::Plain(s) => s.to_owned(),
      Self::Customized(c) => c.to_string(),
    }
  }
}

impl Content for Source {
  fn as_mut_vec(&mut self) -> &mut Vec<u8> {
    match self {
      Plain(s) => unsafe { s.as_mut_vec() },
      Customized(c) => c.as_mut_vec(),
    }
  }
}