\documentclass[aspectratio=169]{beamer}

\usepackage{minted} % unlike listings, this support Rust
\usemintedstyle{vim}

\usepackage{alltt}

% \usepackage{qrcode} doesn't work easily with white-on-black

\usepackage{url}
\usepackage[utf8]{inputenc}

\usepackage{tikz}

\newcommand{\fullimage}[1]{
    % full-screen page from https://tex.stackexchange.com/questions/3915/image-on-full-slide-in-beamer-package#3927
    { % all template changes are local to this group.
        \begin{frame}[plain]
            \begin{tikzpicture}[remember picture,overlay]
                \node[at=(current page.center)] {
                    \includegraphics[height=\paperheight]{#1}
                };
            \end{tikzpicture}
         \end{frame}
    }
}

\usecolortheme{albatross} % easiest base for white-on-black
\setbeamercolor{titlelike}{bg=black,fg=white}
\setbeamercolor{normal text}{bg=black,fg=white}

\title{Using Rust on RIOT OS}
\author{Christian M. Amsüss \url{<ca@etonomy.org>}}
\date{2019-09-05\\RIOT Summit, Helsinki}

\titlegraphic{
  \centering
  \raisebox{-0.2cm}{\includegraphics[width=2cm]{rust-embedded.pdf}\hspace*{1cm}}~%
  \includegraphics[width=4cm]{etonomy.pdf}\hspace*{1cm}~%
  \raisebox{0.5cm}{\includegraphics[width=2cm]{riot.png}}
}

\newcommand{\crate}[1]{\texttt{\href{https://crates.io/crates/#1}{#1}}}

\begin{document}

\frame{\titlepage}

\begin{frame}{Outline}
  \begin{itemize}
    \item The Rust language and ecosystem
    \item Rust on embedded systems
    \item Rust on RIOT
  \end{itemize}
\end{frame}

\fullimage{rust-white.pdf}

\section{The Rust Language}

\begin{frame}[fragile]{Simple things: functions and variables}
  \begin{minted}{rust}
fn fibonacci(n: u32) -> u32 {
    let mut a = 1;
    let mut b = 1;

    for _i in 2..n {
        let old_b = b;

        b = a + b;
        a = old_b;
    }

    b
}
  \end{minted}
\end{frame}


\begin{frame}[fragile]{Structs}
  \begin{minted}{rust}
struct Color {
    r: u8,
    g: u8,
    b: u8
}

let red = Color { r: 255, g: 0, b: 0 };
let h = red.get_hue();
  \end{minted}
\end{frame}


\begin{frame}[fragile]{Enums}
  \begin{minted}{rust}
enum ThreadStatus {
    Stopped,
    Sleeping,
    MutexBlocked,
    ReceiveBlocked,
    // ...
}

enum MaybeThreadStatus {
    Ok(ThreadStatus),
    NotFound
}
  \end{minted}
\end{frame}

\begin{frame}[fragile]{Ownership, Pointers \& References: Ideas}
  \Huge
  \begin{itemize}

    \item \texttt{x} / \texttt{mut x}: owned value

    \item<2-> \texttt{\&mut x}: mutable reference

    \item<2-> \texttt{\&x}: immutable reference

    \item<3-> \texttt{*const x} / \texttt{*mut x}: raw pointers
  \end{itemize}
\end{frame}


\begin{frame}[fragile]{Ownership, Pointers \& References: Example}
  \begin{minted}{rust}
fn receive() -> SharedPktsnip { ... }
fn read_data_from(r: &SharedPktsnip) -> &InternalData { ... }
fn process(input: &InternalData) -> Response { ... }
fn start_write(s: SharedPktsnip) -> WritablePktsnip { ... }
fn set_message(w: &mut WritablePktsnip, data: Response) { ... }

let received: SharedPktsnip = receive();
let data = read_data_from(&received);
let response = process(data);
let mut b = start_write(received);
set_message(&mut b, 42);
  \end{minted}
\end{frame}

\begin{frame}[fragile]{Ownership, Pointers \& References: unsafe}
  \begin{minted}{rust}
  let ptr = 0x40084000 as *mut u32;
  unsafe {
    *ptr = 42;
  }
  \end{minted}
  \bigskip
  \ldots but you're on your own to know that is safe
\end{frame}

\begin{frame}[fragile]{Traits}
  \begin{minted}{rust}
trait OutputPin {
    fn set_low(&mut self);
    fn set_high(&mut self);
}

impl OutputPin for MyPeriph {
    fn set_low(&mut self) {
        self.write_command(SET_LEVEL, 0);
    }

    fn set_high(&mut self) {
        self.write_command(SET_LEVEL, 1);
    }
}
  \end{minted}
\end{frame}

\begin{frame}[fragile]{Generics}
  \begin{minted}{rust}
enum Option<T> {
    Some(T),
    None
}

struct SoftDebouncedButton<P: InputPin> {
    pin: P,
    laststate: bool,
    statecount: u16,
}
  \end{minted}
\end{frame}

\begin{frame}[fragile]{Et cetera ad infinitum}
  \begin{itemize}
    \item Namespacing
    \item Visibility
    \item Closures
    \item Arrays and slices
    \item Standard library
    \item Unicode strings
    \item Futures / asynchronous programming
  \end{itemize}

  \bigskip

  \begin{itemize}
    \item Stable releases
    \item Cargo and the Crates
    \item The Rust Book
  \end{itemize}
\end{frame}

\section{Embedded Rust}

\begin{frame}[fragile]{\crate{embedded-hal}, \texttt{*-hal}, \texttt{*-pac}}
  \begin{columns}
    \begin{column}{0.5\textwidth}\begin{overlayarea}{0.5\textwidth}{0.7\textheight}
      \begin{onlyenv}<1>
        \begin{minted}{rust}
trait OutputPin {
    fn set_low(&mut self);
    fn set_high(&mut self);
}
        \end{minted}
        \end{onlyenv}%
        \begin{onlyenv}<2>
        \begin{minted}{rust}
impl OutputPin for A10 {
    fn set_low(&mut self) {
        self.reg.PA_doutclr.write(|w|
            w.p10().bit(true) });
    }

    fn set_high(&mut self) {
        self.reg.PA_doutset.write(|w|
            w.p10().bit(true) });
    }
}
        \end{minted}
      \end{onlyenv}
      \begin{onlyenv}<3>
% modified from EFR32MG1P133F256GM48.svd
        \begin{minted}{xml}
...
<register>
  <name>PA_DOUT</name>
  <description>Port Data Out
      Set Register</description>
  <addressOffset>0x00C</addressOffset>
  <size>32</size>
  <access>read-write</access>
  <fields>
    <field>
      <name>P0</name>
      ...
        \end{minted}
      \end{onlyenv}
      \begin{onlyenv}<4>
        \begin{minted}{rust}
struct RiotOutputPin {
    pin: gpio_t
}

impl OutputPin for RiotOutputPin {
    fn set_low(&mut self) {
        unsafe {
            gpio_set(self.pin);
        }
    }
}

        \end{minted}
      \end{onlyenv}
    \end{overlayarea}\end{column}

    \begin{column}{0.5\textwidth}
      \Large
      \begin{itemize}
        \item<1-> \crate{embedded-hal}
        \item<2-> various HAL implementations
        \item<3-> various Peripheral Access Crates (PACs)
        \item<4> \vspace{1cm}\crate{riot-wrappers}
        \item<4> \crate{riot-sys}
      \end{itemize}
    \end{column}
  \end{columns}
\end{frame}


\begin{frame}[fragile]{Rust on RIOT in practice: Implementation status}
  \Large
  \begin{itemize}
    \item I$^2$C
    \item SPI
    \item Threads
    \item Gcoap server
    \item shell
    \item GNRC (Pktsnip)
  \end{itemize}

  \bigskip

  + direct hardware access
\end{frame}

\begin{frame}[fragile]{Rust on RIOT in practice: Build system integration}
  \small
  \begin{alltt}
    {\color{gray}$} make BOARD=stk3700 all flash term
    make -C ./RIOT/boards/stk3700
    ...
    make -C ./RIOT/cpu/cortexm_common/periph
    RIOT_CFLAGS="-D... -I ..." cargo build --target arm-... --release
    {\color{green}Compiling} riot-sys v0.2.2
    {\color{green}...}
    {\color{green}Compiling} demo v0.1.0
        {\color{green}Finished} release [optimized + debuginfo] target(s) in 1m 09s
    arm-none-eabi-gcc .../*.o target/.../libdemo.a -o bin/demo.elf
    ### Flashing Target ###
    
    main(): This is RIOT! (Version: ...)
    Hello, world!
    >
  \end{alltt}
\end{frame}

\begin{frame}[fragile]{Rust on RIOT in practice}
  \Large
  in active use

  \bigskip

  benefits on bug prevention and code reuse
\end{frame}

\begin{frame}[fragile]{Future exploration}
  \Large
  \begin{itemize}
    \item More complete mappings
    \item Rust code in packages
    \item Const propagation and bindgen enhancements
  \end{itemize}
\end{frame}

\begin{frame}{}
  \center\Huge{Questions?}
\end{frame}

\begin{frame}{}
    Thanks for your attention

    \vspace{0.5cm}

    Slides and more links on \url{https://christian.amsuess.com/presentations/2019/rust-on-riot/}

    \bigskip
    \center \includegraphics[width=3.2cm]{qr.eps}

    \bigskip
  % FIXME: why is there no \thetitlegraphic?
  \centering
  \raisebox{-0.2cm}{\includegraphics[width=2cm]{rust-embedded.pdf}\hspace*{1cm}}~%
  \includegraphics[width=4cm]{etonomy.pdf}\hspace*{1cm}~%
  \raisebox{0.5cm}{\includegraphics[width=2cm]{riot.png}}
\end{frame}

\end{document}
