common: Add Rust glue for the ringbuf module.
authorTilman Sauerbeck <tilman@code-monkey.de>
Sat, 28 Dec 2019 22:11:09 +0000 (23:11 +0100)
committerTilman Sauerbeck <tilman@code-monkey.de>
Sun, 5 Jan 2020 19:38:11 +0000 (20:38 +0100)
For now this only allows us to access a ringbuffer from Rust
after it has been defined in C code.

SConscript.libcommon
src/common/lib.rs
src/common/ringbuf.rs [new file with mode: 0644]

index e1d378c8384b745f3e1d4944ea0530e5694e4c37..e52b43a88e0820b7ebf6704e47b045f332afe9c8 100644 (file)
@@ -13,6 +13,7 @@ source_files_rs = [
     'src/common/uart.rs',
     'src/common/watchdog.rs',
     'src/common/crc32.rs',
     'src/common/uart.rs',
     'src/common/watchdog.rs',
     'src/common/crc32.rs',
+    'src/common/ringbuf.rs',
     'src/common/buffer.rs',
     'src/common/usb_serial.rs',
     'src/common/display.rs',
     'src/common/buffer.rs',
     'src/common/usb_serial.rs',
     'src/common/display.rs',
index 61d8f797da3887ab3d411db998859e14b22d8e7b..2f7d8944c64935c531bca68061e228f9482d810c 100644 (file)
@@ -35,6 +35,7 @@ pub mod i2c;
 pub mod uart;
 pub mod watchdog;
 pub mod crc32;
 pub mod uart;
 pub mod watchdog;
 pub mod crc32;
+pub mod ringbuf;
 pub mod buffer;
 pub mod usb_serial;
 pub mod screen;
 pub mod buffer;
 pub mod usb_serial;
 pub mod screen;
diff --git a/src/common/ringbuf.rs b/src/common/ringbuf.rs
new file mode 100644 (file)
index 0000000..2599353
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+pub enum Ringbuf {
+}
+
+extern {
+    fn ringbuf_is_empty(ringbuf: *const Ringbuf) -> bool;
+    fn ringbuf_is_full(ringbuf: *const Ringbuf) -> bool;
+    fn ringbuf_write(ringbuf: *mut Ringbuf, value: u8);
+    fn ringbuf_read(ringbuf: *const Ringbuf) -> u8;
+}
+
+impl Ringbuf {
+    pub fn is_empty(&self) -> bool {
+        unsafe { ringbuf_is_empty(self) }
+    }
+
+    pub fn is_full(&self) -> bool {
+        unsafe { ringbuf_is_full(self) }
+    }
+
+    pub fn write(&mut self, value: u8) {
+        unsafe { ringbuf_write(self, value) }
+    }
+
+    pub fn read(&self) -> u8 {
+        unsafe { ringbuf_read(self) }
+    }
+}