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

pub use bitstruct_derive::bitstruct;

pub trait FromRaw<Raw, Target> {
    fn from_raw(raw: Raw) -> Target;
}

pub trait IntoRaw<Raw, Target> {
    fn into_raw(target: Target) -> Raw;
}

/// Blanket impl of FromRaw for any type that has Into<Target> implemented.
/// This allows types that have universal conversions to define Into<Target>
/// rather than a conversion per bitfield. If the Target type does not have a
/// universal representation (i.e. it varies depending on the bitstruct) you
/// should instead implement FromRaw for each particular bitstruct field that
/// contains the Target type.
impl<T, Raw, Target> FromRaw<Raw, Target> for T
where
    Raw: Into<Target>,
{
    fn from_raw(raw: Raw) -> Target {
        raw.into()
    }
}

/// Blanket impl of IntoRaw for any type that has Into<Raw> implemented.
/// This allows types that have universal conversions to define Into<Raw>
/// rather than a conversion per bitfield. If the Target type does not have a
/// universal representation (i.e. it varies depending on the bitstruct) you
/// should instead implement IntoRaw for each particular bitstruct field that
/// contains the Target type.
impl<T, Raw, Target> IntoRaw<Raw, Target> for T
where
    Target: Into<Raw>,
{
    fn into_raw(target: Target) -> Raw {
        target.into()
    }
}