From 73690ab5f75eaa37e4d52bcf64b40b7d3a343c4a Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 08 Jan 2025 13:46:51 +0700 Subject: [PATCH] Don't allocate when encoding commit hashes --- deny.toml | 1 - flake.nix | 3 ++- src/methods/filters.rs | 15 ++++++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/deny.toml b/deny.toml index 54336d3..b3e00dd 100644 --- a/deny.toml +++ a/deny.toml @@ -35,7 +35,6 @@ allow = [] deny = [] skip = [ - { crate = "sync_wrapper@0.1.2", reason = "tower has not upgraded to 1.0 yet" }, { crate = "windows-sys@0.52.0", reason = "gix pulls in two separate versions" }, ] skip-tree = [ diff --git a/flake.nix b/flake.nix index bb80678..d9a8317 100644 --- a/flake.nix +++ a/flake.nix @@ -48,6 +48,7 @@ ./statics ./templates ./themes + ./deny.toml ./build.rs ]; }; @@ -75,7 +76,7 @@ clippy = craneLib.cargoClippy buildArgs; doc = craneLib.cargoDoc buildArgs; audit = craneLib.cargoAudit { inherit advisory-db; src = cargoOnlySrc; }; - deny = craneLib.cargoDeny { src = cargoOnlySrc; }; + deny = craneLib.cargoDeny { inherit src; }; test = craneLib.cargoNextest (buildArgs // { partitions = 1; partitionType = "count"; diff --git a/src/methods/filters.rs b/src/methods/filters.rs index 83a4bce..2724abe 100644 --- a/src/methods/filters.rs +++ a/src/methods/filters.rs @@ -1,8 +1,9 @@ // sorry clippy, we don't have a choice. askama forces this on us #![allow(clippy::unnecessary_wraps, clippy::trivially_copy_pass_by_ref)] use std::{ borrow::Borrow, + fmt::Display, sync::{Arc, LazyLock}, }; @@ -40,8 +41,18 @@ Ok(unix_mode::to_string(u32::from(*s))) } -pub fn hex(s: &[u8]) -> Result { - Ok(const_hex::encode(s)) +pub struct DisplayHexBuffer(const_hex::Buffer); + +impl Display for DisplayHexBuffer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.0.as_str()) + } +} + +pub fn hex(s: &[u8; 20]) -> Result, askama::Error> { + let mut buf = const_hex::Buffer::new(); + buf.format(s); + Ok(DisplayHexBuffer(buf)) } pub fn gravatar(email: &str) -> Result<&'static str, askama::Error> { -- rgit 0.1.4