1use core::fmt::Display;
2use core::ops::{Deref, DerefMut};
3
4use crate::Array;
5
6#[derive(Clone, Copy, Debug, Default)]
21pub struct Writable<A: Array<Item = u8>>(A);
22
23impl<A: Array<Item = u8>> Writable<A> {
24 pub fn as_str(&self) -> &str {
27 core::str::from_utf8(self).unwrap()
28 }
29
30 pub fn as_slice(&self) -> &[u8] {
32 &self.0
33 }
34
35 pub fn as_mut_slice(&mut self) -> &mut [u8] {
37 &mut self.0
38 }
39
40 pub fn unwrap(self) -> A {
42 self.0
43 }
44}
45
46impl<A> Display for Writable<A> where A: Array<Item = u8>
47{
48 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49 write!(f, "{}", self.as_str())
50 }
51}
52
53impl<A: Array<Item = u8>> From<A> for Writable<A> {
54 fn from(a: A) -> Self {
55 Self(a)
56 }
57}
58
59impl<A: Array<Item = u8>> Deref for Writable<A> {
60 type Target = A;
61
62 fn deref(&self) -> &A {
63 &self.0
64 }
65}
66
67impl<A: Array<Item = u8>> DerefMut for Writable<A> {
68 fn deref_mut(&mut self) -> &mut A {
69 &mut self.0
70 }
71}
72
73impl<A: Array<Item = u8>> AsRef<str> for Writable<A> {
74 fn as_ref(&self) -> &str {
75 self.as_str()
76 }
77}
78
79impl<A: Array<Item = u8>> core::fmt::Write for Writable<A> {
80 fn write_str(&mut self, s: &str) -> core::fmt::Result {
81 match A::CAPACITY {
82 | Some(max) if max < self.len() + s.len() => Err(core::fmt::Error),
83 | _ => {
84 self.extend(s.bytes());
85 Ok(())
86 },
87 }
88 }
89}