From b5b01f4dd7ae3ae0a31627630ac752e4c655cee2 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 25 Nov 2023 18:05:55 +0900 Subject: [PATCH] cargo: add ref-cast dependency It helps to implement transparent conversion from &str to &Wrapped(str). We could instead wrap the reference as Wrapped<'a>(&'a str), but it has various drawbacks. Notably we can't implement Borrow and Deref because these traits require a reference in return position. Since the unsafe bits are pretty small, we can instead implement cast functions without using the ref-cast crate. However, I believe we'll trust ref-cast more than hand-crafted unsafe code. https://crates.io/crates/ref-cast https://docs.rs/ref-cast/1.0.20/ref_cast/attr.ref_cast_custom.html --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + lib/Cargo.toml | 1 + 3 files changed, 23 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c4c533784..30dab906f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1681,6 +1681,7 @@ dependencies = [ "rand", "rand_chacha", "rayon", + "ref-cast", "regex", "rustix", "serde", @@ -2380,6 +2381,26 @@ dependencies = [ "thiserror", ] +[[package]] +name = "ref-cast" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acde58d073e9c79da00f2b5b84eed919c8326832648a5b109b3fce1bb1175280" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7473c2cfcf90008193dd0e3e16599455cb601a9fce322b5bb55de799664925" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + [[package]] name = "regex" version = "1.10.2" diff --git a/Cargo.toml b/Cargo.toml index 871eb7a3b..0366a8901 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,7 @@ prost-build = "0.11.9" rand = "0.8.5" rand_chacha = "0.3.1" rayon = "1.8.0" +ref-cast = "1.0.20" regex = "1.10.2" rpassword = "7.3.1" rustix = { version = "0.38.25", features = ["fs"] } diff --git a/lib/Cargo.toml b/lib/Cargo.toml index a9f719b74..9f5d65a4e 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -43,6 +43,7 @@ prost = { workspace = true } rand = { workspace = true } rand_chacha = { workspace = true } rayon = { workspace = true } +ref-cast = { workspace = true } regex = { workspace = true } serde = { workspace = true } serde_json = { workspace = true }