xd_slice/
lib.rs

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
#![no_std]

use core::fmt::{self, Write};
use core::mem;

pub struct Slice<T>([T]);

impl<'a, T> From<&'a [T]> for &'a Slice<T> {
    #[inline]
    fn from(xs: &'a [T]) -> &'a Slice<T> { unsafe { mem::transmute(xs) } }
}

impl<'a, T> From<&'a mut [T]> for &'a mut Slice<T> {
    #[inline]
    fn from(xs: &'a mut [T]) -> &'a mut Slice<T> { unsafe { mem::transmute(xs) } }
}

impl<T: fmt::LowerHex> fmt::LowerHex for Slice<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_char('[')?;
        for (k, x) in self.0.iter().enumerate() {
            if k > 0 { f.write_str(", ")?; }
            fmt::LowerHex::fmt(x, f)?;
        }
        f.write_char(']')?;
        Ok(())
    }
}

impl<T: fmt::UpperHex> fmt::UpperHex for Slice<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_char('[')?;
        for (k, x) in self.0.iter().enumerate() {
            if k > 0 { f.write_str(", ")?; }
            fmt::UpperHex::fmt(x, f)?;
        }
        f.write_char(']')?;
        Ok(())
    }
}