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
//! # To Bytes
//!
//! This module features the [`ToBytes`] trait. That's it.

use std::str;

/// This trait is used in the encosure schemes as the type of `input`.
///
/// It provides the `to_bytes` function that turns the value provided into a `&[u8]`.
pub trait ToBytes {
    /// This function turns `&self` into `&[u8]` (a slice of bytes).
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use aes_rust::to_bytes::ToBytes;
    /// assert_eq!("ABC".to_bytes(), [65, 66, 67]);
    /// assert_eq!([0, 1, 2, 3].to_bytes(), [0, 1, 2, 3]);
    /// ```
    fn to_bytes(&self) -> &[u8];
}

impl ToBytes for &str {
    fn to_bytes(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl ToBytes for String {
    fn to_bytes(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl<const N: usize> ToBytes for [u8; N] {
    fn to_bytes(&self) -> &[u8] {
        self
    }
}
impl<'a, const N: usize> ToBytes for &'a [u8; N] {
    fn to_bytes(&self) -> &[u8] {
        #[allow(clippy::explicit_auto_deref)]
        *self
    }
}

impl ToBytes for &[u8] {
    fn to_bytes(&self) -> &[u8] {
        self
    }
}

impl ToBytes for Vec<u8> {
    fn to_bytes(&self) -> &[u8] {
        self
    }
}