Clean up a few things with the way we use rules_rust

After using rules_rust a bit more, I noticed that some of the ways we
had it set up aren't quite correct. In particular, arch-related `cfg`
didn't work for armv7, which breaks many more complex crates.

Change-Id: I5a15160abfd9da7609f7e9e4b60ec40cbe055d25
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
diff --git a/WORKSPACE b/WORKSPACE
index 8f2dd52..2113efe 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -294,6 +294,9 @@
     #"//tools/cpp:cc-toolchain-cortex-m4f-k22",
     "//tools/python:python_toolchain",
     "//tools/go:noop_go_toolchain",
+    "//tools/rust:rust-toolchain-x86",
+    "//tools/rust:rust-toolchain-armv7",
+    "//tools/rust:rust-toolchain-arm64",
     "//tools/rust:rust-toolchain-roborio",
     "//tools/rust:noop_rust_toolchain",
     "//tools/ts:noop_node_toolchain",
@@ -834,9 +837,9 @@
     path = "third_party/rules_rust",
 )
 
-load("@rules_rust//rust:repositories.bzl", "rust_repository_set")
+load("@rules_rust//rust:repositories.bzl", "rust_toolchain_repository")
 
-rust_repository_set(
+rust_toolchain_repository(
     name = "rust",
     edition = "2021",
     exec_triple = "x86_64-unknown-linux-gnu",
@@ -845,7 +848,9 @@
         "armv7-unknown-linux-gnueabihf",
         "aarch64-unknown-linux-gnu",
     ],
-    version = "1.56.1",
+    rustfmt_version = "1.58.1",
+    toolchain_name_prefix = "toolchain_for",
+    version = "1.58.1",
 )
 
 load("@io_bazel_rules_webtesting//web:repositories.bzl", "web_test_repositories")
diff --git a/build_tests/BUILD b/build_tests/BUILD
index 705c4ff..29a1275 100644
--- a/build_tests/BUILD
+++ b/build_tests/BUILD
@@ -146,9 +146,8 @@
 
 rust_test(
     name = "hello_lib_test",
-    srcs = ["hello_lib.rs"],
+    crate = ":hello_lib",
     target_compatible_with = ["@platforms//os:linux"],
-    deps = [":hello_lib"],
 )
 
 rust_binary(
diff --git a/third_party/rules_rust/rust/platform/platform.bzl b/third_party/rules_rust/rust/platform/platform.bzl
index ca3bef9..de02a89 100644
--- a/third_party/rules_rust/rust/platform/platform.bzl
+++ b/third_party/rules_rust/rust/platform/platform.bzl
@@ -12,6 +12,7 @@
 _SUPPORTED_CPU_ARCH = [
     "aarch64",
     "arm",
+    "armv7",
     "i686",
     "powerpc",
     "s390x",
diff --git a/third_party/rules_rust/rust/platform/triple_mappings.bzl b/third_party/rules_rust/rust/platform/triple_mappings.bzl
index 852ffe7..644f973 100644
--- a/third_party/rules_rust/rust/platform/triple_mappings.bzl
+++ b/third_party/rules_rust/rust/platform/triple_mappings.bzl
@@ -26,6 +26,8 @@
     "aarch64-unknown-linux-gnu",
     "arm-unknown-linux-gnueabi",
     "armv7-unknown-linux-gnueabi",
+    "arm-unknown-linux-gnueabihf",
+    "armv7-unknown-linux-gnueabihf",
     "i686-linux-android",
     "i686-unknown-freebsd",
     "powerpc-unknown-linux-gnu",
diff --git a/tools/rust/BUILD b/tools/rust/BUILD
index a652530..e58bb19 100644
--- a/tools/rust/BUILD
+++ b/tools/rust/BUILD
@@ -1,8 +1,55 @@
 load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain")
 load("@bazel_skylib//rules:write_file.bzl", "write_file")
 
-# Similar to the one automatically generated by @rust, but with the correct
-# hardware platform configured.
+# We have to declare our toolchains individually to get the corect constraints
+# configured so we can robustly select the correct one for each of our
+# platforms.
+
+toolchain(
+    name = "rust-toolchain-x86",
+    exec_compatible_with = [
+        "@platforms//os:linux",
+        "@platforms//cpu:x86_64",
+    ],
+    target_compatible_with = [
+        "@platforms//os:linux",
+        "@platforms//cpu:x86_64",
+    ],
+    toolchain = "@rust//:toolchain_for_x86_64-unknown-linux-gnu_impl",
+    toolchain_type = "@rules_rust//rust:toolchain",
+)
+
+toolchain(
+    name = "rust-toolchain-armv7",
+    exec_compatible_with = [
+        "@platforms//os:linux",
+        "@platforms//cpu:x86_64",
+    ],
+    target_compatible_with = [
+        "@platforms//os:linux",
+        "@platforms//cpu:armv7",
+        # Include this so we're incompatible with the roborio platform, to avoid
+        # subtle order dependencies.
+        "//tools/platforms/hardware:raspberry_pi",
+    ],
+    toolchain = "@rust//:toolchain_for_armv7-unknown-linux-gnueabihf_impl",
+    toolchain_type = "@rules_rust//rust:toolchain",
+)
+
+toolchain(
+    name = "rust-toolchain-arm64",
+    exec_compatible_with = [
+        "@platforms//os:linux",
+        "@platforms//cpu:x86_64",
+    ],
+    target_compatible_with = [
+        "@platforms//os:linux",
+        "@platforms//cpu:arm64",
+    ],
+    toolchain = "@rust//:toolchain_for_aarch64-unknown-linux-gnu_impl",
+    toolchain_type = "@rules_rust//rust:toolchain",
+)
+
 toolchain(
     name = "rust-toolchain-roborio",
     exec_compatible_with = [
@@ -11,6 +58,7 @@
     ],
     target_compatible_with = [
         "@platforms//os:linux",
+        "@platforms//cpu:armv7",
         "//tools/platforms/hardware:roborio",
     ],
     toolchain = "@rust//:toolchain_for_arm-unknown-linux-gnueabi_impl",