-
-
Save Restioson/47976cc261a1a9b788114283e98f2308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated for various serial ports, such as serial0, serial1, ... for each avr somewhere | |
trait SerialRegister { | |
fn send(byte: &u8) -> Result<(), &'static str>; | |
fn receive() -> Result<u8, &'static str>; | |
} | |
struct Serial<P> { | |
_interface: PhantomData<SerialInterface<P>> | |
} | |
// Implementation wrapping serial register | |
impl <P> Serial<P> | |
where P: SerialRegister | |
{ | |
fn new() -> Serial<P> { | |
Serial::<P> { | |
_interface: PhantomData | |
} | |
} | |
fn send(byte: &u8) -> Result<(), &'static str> { | |
P::send() | |
} | |
fn receive() -> Result<u8,&'static str> { | |
P::receive() | |
} | |
} | |
// Generated for each serial port for each avr somewhere | |
mod serial { | |
static Serial0: Serial<Serial0Register> = Serial::<Serial0Register>::new(); | |
static Serial1: Serial<Serial0Register> = Serial::<Serial0Register>::new(); | |
// ... | |
} | |
// then called like | |
for &b in b"Hello World!\n" { | |
serial::Serial0::send(b)?; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment