rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: support arrays in target JSON
@ 2024-07-30  9:26 Alice Ryhl
  2024-07-30  9:56 ` Gary Guo
  2024-08-23  4:46 ` Miguel Ojeda
  0 siblings, 2 replies; 4+ messages in thread
From: Alice Ryhl @ 2024-07-30  9:26 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Matthew Maurer, rust-for-linux, linux-kernel, Alice Ryhl

From: Matthew Maurer <mmaurer@google.com>

Some configuration options such as the supported sanitizer list are
arrays. To support using Rust with sanitizers on x86, we must update the
target.json generator to support this case.

The Push trait is removed in favor of the From trait because the Push
trait doesn't work well in the nested case where you are not really
pushing values to a TargetSpec.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 scripts/generate_rust_target.rs | 82 +++++++++++++++++++++------------
 1 file changed, 53 insertions(+), 29 deletions(-)

diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs
index 87f34925eb7b..c31657380bf9 100644
--- a/scripts/generate_rust_target.rs
+++ b/scripts/generate_rust_target.rs
@@ -20,12 +20,28 @@ enum Value {
     Boolean(bool),
     Number(i32),
     String(String),
+    Array(Vec<Value>),
     Object(Object),
 }
 
 type Object = Vec<(String, Value)>;
 
+fn comma_sep<T>(
+    seq: &[T],
+    formatter: &mut Formatter<'_>,
+    f: impl Fn(&mut Formatter<'_>, &T) -> Result,
+) -> Result {
+    if let [ref rest @ .., ref last] = seq[..] {
+        for v in rest {
+            f(formatter, v)?;
+            formatter.write_str(",")?;
+        }
+        f(formatter, last)?;
+    }
+    Ok(())
+}
+
-/// Minimal "almost JSON" generator (e.g. no `null`s, no arrays, no escaping),
+/// Minimal "almost JSON" generator (e.g. no `null`s, no escaping),
 /// enough for this purpose.
 impl Display for Value {
     fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
@@ -33,59 +49,67 @@ fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
             Value::Boolean(boolean) => write!(formatter, "{}", boolean),
             Value::Number(number) => write!(formatter, "{}", number),
             Value::String(string) => write!(formatter, "\"{}\"", string),
+            Value::Array(values) => {
+                formatter.write_str("[")?;
+                comma_sep(&values[..], formatter, |formatter, v| v.fmt(formatter))?;
+                formatter.write_str("]")
+            }
             Value::Object(object) => {
                 formatter.write_str("{")?;
-                if let [ref rest @ .., ref last] = object[..] {
-                    for (key, value) in rest {
-                        write!(formatter, "\"{}\": {},", key, value)?;
-                    }
-                    write!(formatter, "\"{}\": {}", last.0, last.1)?;
-                }
+                comma_sep(&object[..], formatter, |formatter, v| {
+                    write!(formatter, "\"{}\": {}", v.0, v.1)
+                })?;
                 formatter.write_str("}")
             }
         }
     }
 }
 
+impl From<bool> for Value {
+    fn from(value: bool) -> Self {
+        Self::Boolean(value)
+    }
+}
+
+impl From<i32> for Value {
+    fn from(value: i32) -> Self {
+        Self::Number(value)
+    }
+}
+
+impl From<String> for Value {
+    fn from(value: String) -> Self {
+        Self::String(value)
+    }
+}
+
+impl From<&str> for Value {
+    fn from(value: &str) -> Self {
+        Self::String(value.to_string())
+    }
+}
+
+impl From<Object> for Value {
+    fn from(object: Object) -> Self {
+        Self::Object(object)
+    }
+}
+
+impl<T: Into<Value>, const N: usize> From<[T; N]> for Value {
+    fn from(i: [T; N]) -> Self {
+        Self::Array(i.into_iter().map(|v| v.into()).collect())
+    }
+}
+
 struct TargetSpec(Object);
 
 impl TargetSpec {
     fn new() -> TargetSpec {
         TargetSpec(Vec::new())
     }
+
+    fn push(&mut self, key: &str, value: impl Into<Value>) {
+        self.0.push((key.to_string(), value.into()));
+    }
 }
-
-trait Push<T> {
-    fn push(&mut self, key: &str, value: T);
-}
-
-impl Push<bool> for TargetSpec {
-    fn push(&mut self, key: &str, value: bool) {
-        self.0.push((key.to_string(), Value::Boolean(value)));
-    }
-}
-
-impl Push<i32> for TargetSpec {
-    fn push(&mut self, key: &str, value: i32) {
-        self.0.push((key.to_string(), Value::Number(value)));
-    }
-}
-
-impl Push<String> for TargetSpec {
-    fn push(&mut self, key: &str, value: String) {
-        self.0.push((key.to_string(), Value::String(value)));
-    }
-}
-
-impl Push<&str> for TargetSpec {
-    fn push(&mut self, key: &str, value: &str) {
-        self.push(key, value.to_string());
-    }
-}
-
-impl Push<Object> for TargetSpec {
-    fn push(&mut self, key: &str, value: Object) {
-        self.0.push((key.to_string(), Value::Object(value)));
-    }
-}
 
---
base-commit: 8400291e289ee6b2bf9779ff1c83a291501f017b
change-id: 20240730-target-json-arrays-17c8d1799f9b

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: support arrays in target JSON
  2024-07-30  9:26 [PATCH] rust: support arrays in target JSON Alice Ryhl
@ 2024-07-30  9:56 ` Gary Guo
  2024-07-30 22:33   ` Gatlin Newhouse
  2024-08-23  4:46 ` Miguel Ojeda
  1 sibling, 1 reply; 4+ messages in thread
From: Gary Guo @ 2024-07-30  9:56 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Matthew Maurer, rust-for-linux, linux-kernel

On Tue, 30 Jul 2024 09:26:24 +0000
Alice Ryhl <aliceryhl@google.com> wrote:

> From: Matthew Maurer <mmaurer@google.com>
> 
> Some configuration options such as the supported sanitizer list are
> arrays. To support using Rust with sanitizers on x86, we must update the
> target.json generator to support this case.
> 
> The Push trait is removed in favor of the From trait because the Push
> trait doesn't work well in the nested case where you are not really
> pushing values to a TargetSpec.
> 
> Signed-off-by: Matthew Maurer <mmaurer@google.com>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  scripts/generate_rust_target.rs | 82 +++++++++++++++++++++------------
>  1 file changed, 53 insertions(+), 29 deletions(-)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: support arrays in target JSON
  2024-07-30  9:56 ` Gary Guo
@ 2024-07-30 22:33   ` Gatlin Newhouse
  0 siblings, 0 replies; 4+ messages in thread
From: Gatlin Newhouse @ 2024-07-30 22:33 UTC (permalink / raw)
  To: Gary Guo
  Cc: Alice Ryhl, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Boqun Feng, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Matthew Maurer, rust-for-linux, linux-kernel

On Tue, Jul 30, 2024 at 10:56:46AM UTC, Gary Guo wrote:
> On Tue, 30 Jul 2024 09:26:24 +0000
> Alice Ryhl <aliceryhl@google.com> wrote:
> 
> > From: Matthew Maurer <mmaurer@google.com>
> > 
> > Some configuration options such as the supported sanitizer list are
> > arrays. To support using Rust with sanitizers on x86, we must update the
> > target.json generator to support this case.
> > 
> > The Push trait is removed in favor of the From trait because the Push
> > trait doesn't work well in the nested case where you are not really
> > pushing values to a TargetSpec.
> > 
> > Signed-off-by: Matthew Maurer <mmaurer@google.com>
> > Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> 
> Reviewed-by: Gary Guo <gary@garyguo.net>

Tested-by: Gatlin Newhouse <gatlin.newhouse@gmail.com>

> > ---
> >  scripts/generate_rust_target.rs | 82 +++++++++++++++++++++------------
> >  1 file changed, 53 insertions(+), 29 deletions(-)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: support arrays in target JSON
  2024-07-30  9:26 [PATCH] rust: support arrays in target JSON Alice Ryhl
  2024-07-30  9:56 ` Gary Guo
@ 2024-08-23  4:46 ` Miguel Ojeda
  1 sibling, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2024-08-23  4:46 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Matthew Maurer, rust-for-linux, linux-kernel

On Tue, Jul 30, 2024 at 11:26 AM Alice Ryhl <aliceryhl@google.com> wrote:
>
> From: Matthew Maurer <mmaurer@google.com>
>
> Some configuration options such as the supported sanitizer list are
> arrays. To support using Rust with sanitizers on x86, we must update the
> target.json generator to support this case.
>
> The Push trait is removed in favor of the From trait because the Push
> trait doesn't work well in the nested case where you are not really
> pushing values to a TargetSpec.
>
> Signed-off-by: Matthew Maurer <mmaurer@google.com>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Applied to `rust-next` -- thanks everyone!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-08-23  4:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-30  9:26 [PATCH] rust: support arrays in target JSON Alice Ryhl
2024-07-30  9:56 ` Gary Guo
2024-07-30 22:33   ` Gatlin Newhouse
2024-08-23  4:46 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).