bitstruct/lib.rs
1#![no_std]
2
3pub use bitstruct_derive::bitstruct;
4
5pub trait FromRaw<Raw, Target> {
6    fn from_raw(raw: Raw) -> Target;
7}
8
9pub trait IntoRaw<Raw, Target> {
10    fn into_raw(target: Target) -> Raw;
11}
12
13/// Blanket impl of FromRaw for any type that has Into<Target> implemented.
14/// This allows types that have universal conversions to define Into<Target>
15/// rather than a conversion per bitfield. If the Target type does not have a
16/// universal representation (i.e. it varies depending on the bitstruct) you
17/// should instead implement FromRaw for each particular bitstruct field that
18/// contains the Target type.
19impl<T, Raw, Target> FromRaw<Raw, Target> for T
20where
21    Raw: Into<Target>,
22{
23    fn from_raw(raw: Raw) -> Target {
24        raw.into()
25    }
26}
27
28/// Blanket impl of IntoRaw for any type that has Into<Raw> implemented.
29/// This allows types that have universal conversions to define Into<Raw>
30/// rather than a conversion per bitfield. If the Target type does not have a
31/// universal representation (i.e. it varies depending on the bitstruct) you
32/// should instead implement IntoRaw for each particular bitstruct field that
33/// contains the Target type.
34impl<T, Raw, Target> IntoRaw<Raw, Target> for T
35where
36    Target: Into<Raw>,
37{
38    fn into_raw(target: Target) -> Raw {
39        target.into()
40    }
41}