Loading system/gd/packet/parser/enum_gen.cc +1 −1 Original line number Diff line number Diff line Loading @@ -61,7 +61,7 @@ void EnumGen::GenLogging(std::ostream& stream) { } void EnumGen::GenRustDef(std::ostream& stream) { stream << "#[derive(FromPrimitive, ToPrimitive)]\n"; stream << "#[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)]\n"; stream << "pub enum " << e_.name_ << " {"; for (const auto& pair : e_.constants_) { stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ","; Loading system/gd/packet/parser/gen_rust.cc +1 −1 Original line number Diff line number Diff line Loading @@ -27,7 +27,7 @@ use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive}; use std::convert::TryInto; use thiserror::Error; use std::rc::Rc; use std::sync::Arc; type Result<T> = std::result::Result<T, Error>; Loading system/gd/packet/parser/packet_def.cc +15 −8 Original line number Diff line number Diff line Loading @@ -742,12 +742,14 @@ void PacketDef::GenBuilderConstructor(std::ostream& s) const { void PacketDef::GenRustChildEnums(std::ostream& s) const { if (!children_.empty()) { s << "#[derive(Debug)] "; s << "enum " << name_ << "DataChild {"; for (const auto& child : children_) { s << child->name_ << "(Rc<" << child->name_ << "Data>),"; s << child->name_ << "(Arc<" << child->name_ << "Data>),"; } s << "None,"; s << "}\n"; s << "#[derive(Debug)] "; s << "pub enum " << name_ << "Child {"; for (const auto& child : children_) { s << child->name_ << "(" << child->name_ << "Packet),"; Loading @@ -758,6 +760,7 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const { } void PacketDef::GenRustStructDeclarations(std::ostream& s) const { s << "#[derive(Debug)] "; s << "struct " << name_ << "Data {"; // Generate struct fields Loading @@ -768,16 +771,18 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const { s << "}\n"; // Generate accessor struct s << "#[derive(Debug, Clone)] "; s << "pub struct " << name_ << "Packet {"; auto lineage = GetAncestors(); lineage.push_back(this); for (auto it = lineage.begin(); it != lineage.end(); it++) { auto def = *it; s << util::CamelCaseToUnderScore(def->name_) << ": Rc<" << def->name_ << "Data>,"; s << util::CamelCaseToUnderScore(def->name_) << ": Arc<" << def->name_ << "Data>,"; } s << "}\n"; // Generate builder struct s << "#[derive(Debug)] "; s << "pub struct " << name_ << "Builder {"; auto params = GetParamList().GetFieldsWithoutTypes({ PayloadField::kFieldType, Loading Loading @@ -943,7 +948,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const { s << "impl " << name_ << "Packet {"; if (parent_ == nullptr) { s << "pub fn parse(bytes: &[u8]) -> Result<Self> { "; s << "Ok(Self::new(Rc::new(" << name_ << "Data::parse(bytes)?)))"; s << "Ok(Self::new(Arc::new(" << name_ << "Data::parse(bytes)?)))"; s << "}"; } auto root = GetRootDef(); Loading @@ -955,12 +960,14 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const { s << " buffer.freeze()"; s << "}\n"; s << "pub fn to_vec(self) -> Vec<u8> { self.to_bytes().to_vec() }\n"; if (!children_.empty()) { s << " pub fn specialize(self) -> " << name_ << "Child {"; s << " match self." << util::CamelCaseToUnderScore(name_) << ".child {"; s << " pub fn specialize(&self) -> " << name_ << "Child {"; s << " match &self." << util::CamelCaseToUnderScore(name_) << ".child {"; for (const auto& child : children_) { s << name_ << "DataChild::" << child->name_ << "(_) => " << name_ << "Child::" << child->name_ << "(" << child->name_ << "Packet::new(self." << root_accessor << ")),"; << child->name_ << "Packet::new(self." << root_accessor << ".clone())),"; } s << name_ << "DataChild::None => " << name_ << "Child::None,"; s << "}}"; Loading @@ -969,7 +976,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const { lineage.push_back(this); const ParentDef* prev = nullptr; s << " fn new(root: Rc<" << root->name_ << "Data>) -> Self {"; s << " fn new(root: Arc<" << root->name_ << "Data>) -> Self {"; for (auto it = lineage.begin(); it != lineage.end(); it++) { auto def = *it; auto accessor_name = util::CamelCaseToUnderScore(def->name_); Loading Loading @@ -1044,7 +1051,7 @@ void PacketDef::GenRustBuilderStructImpls(std::ostream& s) const { }); auto accessor_name = util::CamelCaseToUnderScore(ancestor->name_); s << "let " << accessor_name << "= Rc::new(" << ancestor->name_ << "Data {"; s << "let " << accessor_name << "= Arc::new(" << ancestor->name_ << "Data {"; for (auto field : fields) { auto constraint = all_constraints.find(field->GetName()); s << field->GetName() << ": "; Loading system/gd/packet/parser/struct_def.cc +1 −0 Original line number Diff line number Diff line Loading @@ -358,6 +358,7 @@ void StructDef::GenRustSizeField(std::ostream& s) const { } void StructDef::GenRustDeclarations(std::ostream& s) const { s << "#[derive(Debug)] "; s << "pub struct " << name_ << "{"; // Generate struct fields Loading system/gd/rust/hal/Android.bp +5 −1 Original line number Diff line number Diff line Loading @@ -5,8 +5,8 @@ rust_library { srcs: ["src/lib.rs"], edition: "2018", rustlibs: [ "libbt_packet", "libbt_facade_proto", "libbt_packets", "libbytes", "libfutures", "libthiserror", Loading @@ -19,6 +19,10 @@ rust_library { "liblazy_static", "liblog_rust", "libbt_common", "libnum_traits", ], proc_macros: [ "libnum_derive", ], target: { android: { Loading Loading
system/gd/packet/parser/enum_gen.cc +1 −1 Original line number Diff line number Diff line Loading @@ -61,7 +61,7 @@ void EnumGen::GenLogging(std::ostream& stream) { } void EnumGen::GenRustDef(std::ostream& stream) { stream << "#[derive(FromPrimitive, ToPrimitive)]\n"; stream << "#[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)]\n"; stream << "pub enum " << e_.name_ << " {"; for (const auto& pair : e_.constants_) { stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ","; Loading
system/gd/packet/parser/gen_rust.cc +1 −1 Original line number Diff line number Diff line Loading @@ -27,7 +27,7 @@ use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive}; use std::convert::TryInto; use thiserror::Error; use std::rc::Rc; use std::sync::Arc; type Result<T> = std::result::Result<T, Error>; Loading
system/gd/packet/parser/packet_def.cc +15 −8 Original line number Diff line number Diff line Loading @@ -742,12 +742,14 @@ void PacketDef::GenBuilderConstructor(std::ostream& s) const { void PacketDef::GenRustChildEnums(std::ostream& s) const { if (!children_.empty()) { s << "#[derive(Debug)] "; s << "enum " << name_ << "DataChild {"; for (const auto& child : children_) { s << child->name_ << "(Rc<" << child->name_ << "Data>),"; s << child->name_ << "(Arc<" << child->name_ << "Data>),"; } s << "None,"; s << "}\n"; s << "#[derive(Debug)] "; s << "pub enum " << name_ << "Child {"; for (const auto& child : children_) { s << child->name_ << "(" << child->name_ << "Packet),"; Loading @@ -758,6 +760,7 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const { } void PacketDef::GenRustStructDeclarations(std::ostream& s) const { s << "#[derive(Debug)] "; s << "struct " << name_ << "Data {"; // Generate struct fields Loading @@ -768,16 +771,18 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const { s << "}\n"; // Generate accessor struct s << "#[derive(Debug, Clone)] "; s << "pub struct " << name_ << "Packet {"; auto lineage = GetAncestors(); lineage.push_back(this); for (auto it = lineage.begin(); it != lineage.end(); it++) { auto def = *it; s << util::CamelCaseToUnderScore(def->name_) << ": Rc<" << def->name_ << "Data>,"; s << util::CamelCaseToUnderScore(def->name_) << ": Arc<" << def->name_ << "Data>,"; } s << "}\n"; // Generate builder struct s << "#[derive(Debug)] "; s << "pub struct " << name_ << "Builder {"; auto params = GetParamList().GetFieldsWithoutTypes({ PayloadField::kFieldType, Loading Loading @@ -943,7 +948,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const { s << "impl " << name_ << "Packet {"; if (parent_ == nullptr) { s << "pub fn parse(bytes: &[u8]) -> Result<Self> { "; s << "Ok(Self::new(Rc::new(" << name_ << "Data::parse(bytes)?)))"; s << "Ok(Self::new(Arc::new(" << name_ << "Data::parse(bytes)?)))"; s << "}"; } auto root = GetRootDef(); Loading @@ -955,12 +960,14 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const { s << " buffer.freeze()"; s << "}\n"; s << "pub fn to_vec(self) -> Vec<u8> { self.to_bytes().to_vec() }\n"; if (!children_.empty()) { s << " pub fn specialize(self) -> " << name_ << "Child {"; s << " match self." << util::CamelCaseToUnderScore(name_) << ".child {"; s << " pub fn specialize(&self) -> " << name_ << "Child {"; s << " match &self." << util::CamelCaseToUnderScore(name_) << ".child {"; for (const auto& child : children_) { s << name_ << "DataChild::" << child->name_ << "(_) => " << name_ << "Child::" << child->name_ << "(" << child->name_ << "Packet::new(self." << root_accessor << ")),"; << child->name_ << "Packet::new(self." << root_accessor << ".clone())),"; } s << name_ << "DataChild::None => " << name_ << "Child::None,"; s << "}}"; Loading @@ -969,7 +976,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const { lineage.push_back(this); const ParentDef* prev = nullptr; s << " fn new(root: Rc<" << root->name_ << "Data>) -> Self {"; s << " fn new(root: Arc<" << root->name_ << "Data>) -> Self {"; for (auto it = lineage.begin(); it != lineage.end(); it++) { auto def = *it; auto accessor_name = util::CamelCaseToUnderScore(def->name_); Loading Loading @@ -1044,7 +1051,7 @@ void PacketDef::GenRustBuilderStructImpls(std::ostream& s) const { }); auto accessor_name = util::CamelCaseToUnderScore(ancestor->name_); s << "let " << accessor_name << "= Rc::new(" << ancestor->name_ << "Data {"; s << "let " << accessor_name << "= Arc::new(" << ancestor->name_ << "Data {"; for (auto field : fields) { auto constraint = all_constraints.find(field->GetName()); s << field->GetName() << ": "; Loading
system/gd/packet/parser/struct_def.cc +1 −0 Original line number Diff line number Diff line Loading @@ -358,6 +358,7 @@ void StructDef::GenRustSizeField(std::ostream& s) const { } void StructDef::GenRustDeclarations(std::ostream& s) const { s << "#[derive(Debug)] "; s << "pub struct " << name_ << "{"; // Generate struct fields Loading
system/gd/rust/hal/Android.bp +5 −1 Original line number Diff line number Diff line Loading @@ -5,8 +5,8 @@ rust_library { srcs: ["src/lib.rs"], edition: "2018", rustlibs: [ "libbt_packet", "libbt_facade_proto", "libbt_packets", "libbytes", "libfutures", "libthiserror", Loading @@ -19,6 +19,10 @@ rust_library { "liblazy_static", "liblog_rust", "libbt_common", "libnum_traits", ], proc_macros: [ "libnum_derive", ], target: { android: { Loading