From 520357e78b6b3d0ef517350259fe240d82d3e2b1 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 3 Nov 2023 13:25:27 +0100 Subject: [PATCH] jose: Reduce stack size of JsonWebSignatureHeader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … by putting the optional jwk field behind a box. The overall size will be one pointer larger when the field is Some(_), but more than 300 bytes small when it is None. --- crates/jose/src/jwt/header.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/jose/src/jwt/header.rs b/crates/jose/src/jwt/header.rs index af2ad424..6579042d 100644 --- a/crates/jose/src/jwt/header.rs +++ b/crates/jose/src/jwt/header.rs @@ -28,7 +28,7 @@ pub struct JsonWebSignatureHeader { jku: Option, #[serde(default)] - jwk: Option, + jwk: Option>, #[serde(default)] kid: Option, @@ -91,12 +91,16 @@ impl JsonWebSignatureHeader { #[must_use] pub const fn jwk(&self) -> Option<&PublicJsonWebKey> { - self.jwk.as_ref() + // Can't use as_deref because it's not a const fn + match &self.jwk { + Some(jwk) => Some(jwk), + None => None, + } } #[must_use] pub fn with_jwk(mut self, jwk: PublicJsonWebKey) -> Self { - self.jwk = Some(jwk); + self.jwk = Some(Box::new(jwk)); self }