Skip to content

Instantly share code, notes, and snippets.

@istankovic
Last active November 20, 2016 19:57
Show Gist options
  • Save istankovic/4652922b59558c1110884502a66b0410 to your computer and use it in GitHub Desktop.
Save istankovic/4652922b59558c1110884502a66b0410 to your computer and use it in GitHub Desktop.
struct Unconnected<T: I2c>(T);
struct Connected<T: I2c>(T);
impl<T: I2c> Unconnected<T> {
#[inline(always)]
fn start(self) -> Result<Connected<T>, T::Err> {
self.0.start()
}
}
impl<T: I2c> Connected<T> {
#[inline(always)]
fn read(&mut self) -> Result<u8, T::Err> {
self.0.read()
}
#[inline(always)]
fn stop(self) -> Result<Unconnected<T>, T::Err> {
self.0.stop()
}
}
trait I2c: Sized {
type Err;
fn start(self) -> Result<Connected<Self>, Self::Err>;
fn stop(self) -> Result<Unconnected<Self>, Self::Err>;
fn read(&mut self) -> Result<u8, Self::Err>;
}
struct SAMDI2c;
impl I2c for SAMDI2c {
type Err = ();
fn read(&mut self) -> Result<u8, Self::Err> {
Ok(8)
}
fn start(self) -> Result<Connected<SAMDI2c>, ()> {
Ok(Connected(SAMDI2c))
}
fn stop(self) -> Result<Unconnected<SAMDI2c>, ()> {
Ok(Unconnected(self))
}
}
#[allow(unused_variables)]
fn main() {
let x = SAMDI2c;
let mut y = x.start().unwrap();
let r = y.read();
let r = y.read();
let x = y.stop().unwrap();
let z = x.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment