Skip to content

Commit

Permalink
Rust: add support for no_std env
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Sep 12, 2024
1 parent b7bc57e commit ecf547f
Show file tree
Hide file tree
Showing 26 changed files with 2,887 additions and 1,612 deletions.
24 changes: 12 additions & 12 deletions lib/xdrgen/generators/rust.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def render_enum_of_all_types(out, types)
pub const VARIANTS: [TypeVariant; #{types.count}] = [ #{types.map { |t| "TypeVariant::#{t}," }.join("\n")} ];
pub const VARIANTS_STR: [&'static str; #{types.count}] = [ #{types.map { |t| "\"#{t}\"," }.join("\n")} ];
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
#[allow(clippy::too_many_lines)]
pub fn read_xdr<R: Read>(v: TypeVariant, r: &mut Limited<R>) -> Result<Self> {
match v {
Expand All @@ -190,7 +190,7 @@ def render_enum_of_all_types(out, types)
Ok(t)
}
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
pub fn read_xdr_to_end<R: Read>(v: TypeVariant, r: &mut Limited<R>) -> Result<Self> {
let s = Self::read_xdr(v, r)?;
// Check that any further reads, such as this read of one byte, read no
Expand Down Expand Up @@ -234,7 +234,7 @@ def render_enum_of_all_types(out, types)
}
}
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
pub fn from_xdr<B: AsRef<[u8]>>(v: TypeVariant, bytes: B, limits: Limits) -> Result<Self> {
let mut cursor = Limited::new(Cursor::new(bytes.as_ref()), limits);
let t = Self::read_xdr_to_end(v, &mut cursor)?;
Expand Down Expand Up @@ -318,7 +318,7 @@ def render_enum_of_all_types(out, types)
}
impl WriteXdr for Type {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
#[allow(clippy::too_many_lines)]
fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<()> {
match self {
Expand Down Expand Up @@ -407,7 +407,7 @@ def render_struct(out, struct)
out.puts ""
out.puts <<-EOS.strip_heredoc
impl ReadXdr for #{name struct} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self> {
r.with_limited_depth(|r| {
Ok(Self{
Expand All @@ -420,7 +420,7 @@ def render_struct(out, struct)
}
impl WriteXdr for #{name struct} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<()> {
w.with_limited_depth(|w| {
#{struct.members.map do |m|
Expand Down Expand Up @@ -517,7 +517,7 @@ def render_enum(out, enum)
}
impl ReadXdr for #{name enum} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self> {
r.with_limited_depth(|r| {
let e = i32::read_xdr(r)?;
Expand All @@ -528,7 +528,7 @@ def render_enum(out, enum)
}
impl WriteXdr for #{name enum} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<()> {
w.with_limited_depth(|w| {
let i: i32 = (*self).into();
Expand Down Expand Up @@ -656,7 +656,7 @@ def render_union(out, union)
impl Union<#{discriminant_type}> for #{name union} {}
impl ReadXdr for #{name union} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self> {
r.with_limited_depth(|r| {
let dv: #{discriminant_type} = <#{discriminant_type} as ReadXdr>::read_xdr(r)?;
Expand All @@ -678,7 +678,7 @@ def render_union(out, union)
}
impl WriteXdr for #{name union} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<()> {
w.with_limited_depth(|w| {
self.discriminant().write_xdr(w)?;
Expand Down Expand Up @@ -816,7 +816,7 @@ def render_typedef(out, typedef)
}
impl ReadXdr for #{name typedef} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self> {
r.with_limited_depth(|r| {
let i = #{reference_to_call(typedef, typedef.type)}::read_xdr(r)?;
Expand All @@ -827,7 +827,7 @@ def render_typedef(out, typedef)
}
impl WriteXdr for #{name typedef} {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<()> {
w.with_limited_depth(|w|{ self.0.write_xdr(w) })
}
Expand Down
Loading

0 comments on commit ecf547f

Please sign in to comment.