Skip to content

Instantly share code, notes, and snippets.

@Restioson
Last active August 30, 2017 12:34
Show Gist options
  • Save Restioson/47976cc261a1a9b788114283e98f2308 to your computer and use it in GitHub Desktop.
Save Restioson/47976cc261a1a9b788114283e98f2308 to your computer and use it in GitHub Desktop.
// 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