Wrap //aos:uuid for Rust

Including conversions to the native Rust uuid crate's types.

Change-Id: Id8f87c60df4f5c9daa8ce4f72c65b376b0ec812a
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
diff --git a/aos/BUILD b/aos/BUILD
index 5fcb7cf..8e223b7 100644
--- a/aos/BUILD
+++ b/aos/BUILD
@@ -695,6 +695,30 @@
     ],
 )
 
+cc_library(
+    name = "uuid_for_rust",
+    hdrs = ["uuid_for_rust.h"],
+    deps = [
+        ":uuid",
+    ],
+)
+
+autocxx_library(
+    name = "uuid_rs",
+    srcs = ["uuid.rs"],
+    crate_name = "aos_uuid",
+    libs = [
+        ":uuid",
+        ":uuid_for_rust",
+    ],
+    override_cc_toolchain = "@llvm_toolchain//:cc-clang-x86_64-linux",
+    rs_deps = [
+        "//third_party/cargo:uuid",
+    ],
+    target_compatible_with = ["//tools/platforms/rust:has_support"],
+    visibility = ["//visibility:public"],
+)
+
 cc_binary(
     name = "aos_graph_channels",
     srcs = [
diff --git a/aos/uuid.rs b/aos/uuid.rs
new file mode 100644
index 0000000..737d6f4
--- /dev/null
+++ b/aos/uuid.rs
@@ -0,0 +1,30 @@
+autocxx::include_cpp! (
+#include "aos/uuid.h"
+#include "aos/uuid_for_rust.h"
+
+safety!(unsafe)
+
+generate_pod!("aos::UUID")
+);
+
+pub use ffi::aos::UUID;
+
+impl From<UUID> for uuid::Uuid {
+    fn from(uuid: UUID) -> uuid::Uuid {
+        uuid::Uuid::from_bytes(uuid.data)
+    }
+}
+
+impl AsRef<uuid::Uuid> for UUID {
+    fn as_ref(&self) -> &uuid::Uuid {
+        uuid::Uuid::from_bytes_ref(&self.data)
+    }
+}
+
+impl From<uuid::Uuid> for UUID {
+    fn from(uuid: uuid::Uuid) -> UUID {
+        UUID {
+            data: *uuid.as_bytes(),
+        }
+    }
+}
diff --git a/aos/uuid_for_rust.h b/aos/uuid_for_rust.h
new file mode 100644
index 0000000..5bbddc8
--- /dev/null
+++ b/aos/uuid_for_rust.h
@@ -0,0 +1,20 @@
+#ifndef AOS_UUID_FOR_RUST_H_
+#define AOS_UUID_FOR_RUST_H_
+
+#include "aos/uuid.h"
+
+namespace aos {
+
+// An alternative version of UUID to feed autocxx, to work around
+// https://github.com/google/autocxx/issues/266.
+/// <div rustbindgen replaces="aos::UUID"></div>
+struct RustUUID {
+  uint8_t data[16];
+};
+
+static_assert(sizeof(UUID) == sizeof(RustUUID));
+static_assert(alignof(UUID) == alignof(RustUUID));
+
+}  // namespace aos
+
+#endif  // AOS_UUID_FOR_RUST_H_