From 08d63864820676d213434c15df8c467b734bc7fa Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 4 Nov 2019 17:49:33 +0000 Subject: [PATCH] acpi_tables: aml: Add support for strings Support strings from both static strings (&'static str) and String. Signed-off-by: Rob Bradford --- acpi_tables/src/aml.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/acpi_tables/src/aml.rs b/acpi_tables/src/aml.rs index 1c04f7734..f6aaf0946 100644 --- a/acpi_tables/src/aml.rs +++ b/acpi_tables/src/aml.rs @@ -285,6 +285,30 @@ impl Aml for Usize { } } +fn create_aml_string(v: &str) -> Vec { + let mut data = Vec::new(); + data.push(0x0D); /* String Op */ + data.extend_from_slice(v.as_bytes()); + data.push(0x0); /* NullChar */ + data +} + +pub type AmlStr = &'static str; + +impl Aml for AmlStr { + fn to_aml_bytes(&self) -> Vec { + create_aml_string(self) + } +} + +pub type AmlString = String; + +impl Aml for AmlString { + fn to_aml_bytes(&self) -> Vec { + create_aml_string(self) + } +} + pub struct ResourceTemplate<'a> { children: Vec<&'a dyn Aml>, } @@ -991,4 +1015,16 @@ mod tests { ] ); } + + #[test] + fn test_string() { + assert_eq!( + (&"ACPI" as &dyn Aml).to_aml_bytes(), + [0x0d, b'A', b'C', b'P', b'I', 0] + ); + assert_eq!( + "ACPI".to_owned().to_aml_bytes(), + [0x0d, b'A', b'C', b'P', b'I', 0] + ); + } }