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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::{write_into, WriteInto};
use std::io;
use std::mem::size_of;
use std::slice::from_raw_parts;

/// Used to write values as they are represented in memory.
///
/// # Examples
///
/// Writing struct into a sink.
///
/// ```
/// use write_into::{Plain, write_into};
///
/// struct Rgba {
///     r: u8,
///     g: u8,
///     b: u8,
///     a: u8,
/// }
///
/// let color = Rgba { r: 0x18, g: 0x18, b: 0x18, a: 0xFF };
/// let mut buffer = Vec::new();
/// write_into(&mut buffer, Plain(&color)).unwrap();
/// assert_eq!(&buffer, &[0x18, 0x18, 0x18, 0xFF]);
/// ```
///
/// Writing array into a sink.
///
/// ```
/// use write_into::{Plain, write_into};
///
/// let bytes: &[u8; 4] = b"\0asm";
/// let mut buffer = Vec::new();
/// write_into(&mut buffer, Plain(bytes)).unwrap();
/// assert_eq!(&buffer, b"\0asm");
/// ```
///
/// Writing slice into a sink (the crate also provides implementation for [`Plain<&str>`]).
///
/// ```
/// use write_into::{Plain, write_into};
///
/// let bytes: &[u8] = b"([Ljava/lang/String;)V";
/// let mut buffer = Vec::new();
/// write_into(&mut buffer, Plain(bytes)).unwrap();
/// assert_eq!(&buffer, b"([Ljava/lang/String;)V");
/// ```
pub struct Plain<T>(pub T);

/// Transmutes arbitrary value into a byte slice.
impl<T> WriteInto for Plain<&T> {
    type Output = ();

    fn write_into(self, sink: &mut impl io::Write) -> io::Result<Self::Output> {
        // SAFETY:
        // - The slice points to a memory occupied by the data.
        // - The data is immutably borrowed.
        let bytes = unsafe {
            let data = self.0 as *const T as *const u8;
            from_raw_parts(data, size_of::<T>())
        };

        sink.write_all(&bytes)?;
        Ok(())
    }
}

impl<T> WriteInto for &Plain<&T> {
    type Output = ();

    fn write_into(self, sink: &mut impl io::Write) -> io::Result<Self::Output> {
        write_into(sink, Plain(self.0))
    }
}

/// Transmutes arbitrary slice into a byte slice.
impl<T> WriteInto for Plain<&[T]> {
    type Output = ();

    fn write_into(self, sink: &mut impl io::Write) -> io::Result<Self::Output> {
        // SAFETY:
        // - The slice points to a memory occupied by the data.
        // - The data is immutably borrowed.
        let bytes = unsafe {
            let data = self.0 as *const [T] as *const u8;
            from_raw_parts(data, self.0.len() * size_of::<T>())
        };

        sink.write_all(&bytes)?;
        Ok(())
    }
}

impl<T> WriteInto for &Plain<&[T]> {
    type Output = ();

    fn write_into(self, sink: &mut impl io::Write) -> io::Result<Self::Output> {
        write_into(sink, Plain(self.0))
    }
}

impl WriteInto for Plain<&str> {
    type Output = ();

    fn write_into(self, sink: &mut impl io::Write) -> io::Result<Self::Output> {
        sink.write_all(self.0.as_bytes())?;
        Ok(())
    }
}

impl WriteInto for &Plain<&str> {
    type Output = ();

    fn write_into(self, sink: &mut impl io::Write) -> io::Result<Self::Output> {
        write_into(sink, Plain(self.0))
    }
}

#[cfg(test)]
mod tests {
    use super::super::*;
    use super::*;

    #[test]
    fn write_u8() {
        let mut buffer = Vec::new();
        write_into(&mut buffer, Plain(&0x7Fu8)).unwrap();
        assert_eq!(&buffer, &[0x7F]);
    }

    #[test]
    fn write_str() {
        let bytes = "([Ljava/lang/String;)V";
        let mut buffer = Vec::new();
        write_into(&mut buffer, Plain(bytes)).unwrap();
        assert_eq!(&buffer, b"([Ljava/lang/String;)V");
    }

    #[test]
    fn write_slice_of_arrays() {
        let bytes: &[[u8; 2]] = &[[0x01, 0x02], [0x03, 0x04]];
        let mut buffer = Vec::new();
        write_into(&mut buffer, Plain(bytes)).unwrap();
        assert_eq!(&buffer, &[0x01, 0x02, 0x03, 0x04]);
    }
}

macro_rules! impl_write_into {
    ($($primitive:ty)*) => {
        $(
            impl WriteInto for Plain<$primitive> {
                type Output = ();

                fn write_into(self, sink: &mut impl io::Write) -> io::Result<Self::Output> {
                    write_into(sink, Plain(&self.0))
                }
            }
        )*
    };
}

impl_write_into! {
    i8 i16 i32 i64 i128 isize
    u8 u16 u32 u64 u128 usize
    bool char f32 f64
}