Repository: alex/rust-asn1 (language: rust)

Issue:
Support writing with fallible allocators
This will involve all the write functions needing to change to return a `Result`.


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f603536..54b333a 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -50,8 +50,14 @@ jobs:
           # MSRV
           - VERSION: "1.47.0"
             FLAGS: "--no-default-features --features std,derive"
+          # MSRV for the fallible-allocations features
+          - VERSION: "1.57.0"
+            FLAGS: "--features fallible-allocations"
+
           - VERSION: stable
             FLAGS: ""
+          - VERSION: stable
+            FLAGS: "--features fallible-allocations"
           - VERSION: stable
             FLAGS: "--no-default-features --features std"
           - VERSION: stable
diff --git a/Cargo.toml b/Cargo.toml
index 47dbcb5..24ef9b9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,7 @@ edition = "2018"
 default = ["std", "derive", "const-generics"]
 std = []
 const-generics = []
+fallible-allocations = []
 derive = ["asn1_derive"]
 
 [dependencies]
diff --git a/src/lib.rs b/src/lib.rs
index 17747fb..b5dde15 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -97,6 +97,13 @@
 //! ```
 //!
 //! All variants must have a single un-named field.
+//!
+//! # Fallible allocations
+//!
+//! `asn1::write` and `asn1::write_single` emit a `Vec<u8>` containing the
+//! serialized DER data. If you would like to be able to handle allocation
+//! failures when writing data, specify the `fallible-allocations` feature of
+//! this crate. This feature require Rust 1.57 or greater.
 
 extern crate alloc;
 
diff --git a/src/writer.rs b/src/writer.rs
index 9502e4e..17250f9 100644
--- a/src/writer.rs
+++ b/src/writer.rs
@@ -5,9 +5,10 @@ use alloc::vec::Vec;
 
 /// `WriteError` are returned when there is an error writing the ASN.1 data.
 ///
-/// Note that `AllocationError` (and thus `WriteError` as a whole) is never
-/// produced, but its expected in the future that as Rust's support for
-/// fallible allocations improves that it will be.
+/// Note that `AllocationError` (and thus `WriteError` as a whole) is only
+/// produced when the `fallible-allocations` feature is used. It's expected
+/// that in the future that as this crate's MSRV increases and Rust's support
+/// for fallible allocations improves that this will be the default.
 #[derive(PartialEq, Eq, Debug)]
 pub enum WriteError {
     AllocationError,
@@ -40,12 +41,22 @@ impl WriteBuf {
 
     #[inline]
     pub fn push_byte(&mut self, b: u8) -> WriteResult {
+        #[cfg(feature = "fallible-allocattions")]
+        self.0
+            .try_reserve(1)
+            .map_err(|_| WriteError::AllocationError)?;
+
         self.0.push(b);
         Ok(())
     }
 
     #[inline]
     pub fn push_slice(&mut self, data: &[u8]) -> WriteResult {
+        #[cfg(feature = "fallible-allocattions")]
+        self.0
+            .try_reserve(data.len())
+            .map_err(|_| WriteError::AllocationError)?;
+
         self.0.extend_from_slice(data);
         Ok(())
     }


Test patch: