From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7A00242D62 for ; Sun, 3 May 2026 17:14:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777828483; cv=none; b=jnjFrr5JP/hRiUiWpe7QgHFIU91uk8gHUmaavjFIEcsJ2ujAsUuPGm7fFKMGONEp+OBBeJ8dXRglQLwvfBu+3xwczQk2B9AqQGxBIL91t8LJkvAIk5M9WUvXmMP72JhBMgO7rPPdXm67NBdH82d3jpY7Sb6mU6Dwy9Qsny2e4to= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777828483; c=relaxed/simple; bh=tt4grDoBvMrEF7yzofX0or4CRlzRgZF6uCIitrQowKo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JJBT0jWW2wx9GFvagv7R4lPL5m9V9xpGz544CRwfyMJ6BqHq0GjVzkfcONs4xb2hbyyXn82hS6NG7kYlq7kDHa17yEXAL7fJS/dBlw35MwOzUk69rJa8YEvj7I6iPs/tavUwL9A7rd6p2f1SDPvn22wfDe2guoF9q1gCRi8L4Rw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iAFIfSQw; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iAFIfSQw" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3653cb9c6f8so247676a91.0 for ; Sun, 03 May 2026 10:14:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777828481; x=1778433281; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=uuwdaXnXwhmjY74E/KqwIa62pTOOtHee6TZaGFXH7LQ=; b=iAFIfSQwbHCKMbjL6l5rRMgEtKF+fpXNdTI9GTeavLGBoR3yRhuEznHaD7qVUwIJY+ wbb5F7QKqdlDyoXU2dmxxKRqDETOf98PMMXqv61v3X+zVJHJ33ky3sxaMT8bSaqbBS2c q+n37eqitcf8q4rVYKO+/3gPNcpCxkahb2zYx7tBExV3usY1yo699JbcDrkZMO6Up9I5 HIBx1veiePEPSFm7N1L8if1vyqVOsxdM/ed7wuvVq2kueXhHcOOgMFJG1QngZAg/FwjP uxAAVk13ZGF5I0n1YEJKOul1AcX8rIdK4g9kzrMfBIQYZTN3E8NXwpYorcyNA3IngMCr V0jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777828481; x=1778433281; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=uuwdaXnXwhmjY74E/KqwIa62pTOOtHee6TZaGFXH7LQ=; b=KylnYEmRmLw8dkU7g0UzCqzrtqpHypS06HOiX/go74bO0hsRDgS7gC6yodV34+LTbM xxuggTCQjaK/1khH4r34KDr48QxnljrjW+e8IBFg2//ixI0nQT/ACazS+gd6/bzcQCDd SYWeytJ9SO/yxCiANUwtM1JRG26rRTnBJQRpaBEaWCL6UqCkHJV8pO+Iq1V5cP82RooA Lb+TtayhMhEgHfcSw2wOv7rxLwMuE0C/Mz87pEijvNPjdgcSFcQ74tHe70gfQctt9ItD rmvc+bg7fAeR0Zp6GtaMqJxoo/N4O9qCuUyBSxg8G2TLLIDfYTVLuBuit2FRxCk0IfPt caJA== X-Forwarded-Encrypted: i=1; AFNElJ8n/NLgklIz5Y4zHOJ3lQcxe8Lv9u9ksH4HIHV7wtOV9MtbcutDFGLOjIpKKpWQ6j1eOcfxJKeP1kb9dYI=@vger.kernel.org X-Gm-Message-State: AOJu0YzlMOP0JXmyfFQtPBRgU53Idfwp1KMepngvt8JXEtb1e5vvqUtv y0JJ5kzQPYASHolySQcjwa9dqjeZn23G5N22fZq6jMIEyoiH+nGaj82j X-Gm-Gg: AeBDievBZlnlrGVlsCLJYSeiaFQJTILw+/9HPiXYHCDn4wIqWMx+BDAyag4Hhun1xjy pVpVPga4Trd5MFQ6CZ4iZvkWuxe1U3jui/qkn11rcNuysX7St23NnEQfcui5ooQ5EwtBvWoLU6s qFfPGSUwjTJL2Yb34bEdhg8CAviH6DxCwR0RJhWdUXP68MWINYyC2wUF/2xH1xdguxHxqumLsjI 30hKY/ihlxs+YKQPRz7QrX/21YVN2rSqpWmxbVw7nBmq4vGCuyyvhNjq/u7ioVammVMufaWKlKx tqTRjfLDXzLy3/gKZbmZOWjnhtxXXdLFDgu0EfgzErUi7FOHUv/ruyEdf71TIxvG9gBUBQVUCld D1IXbS4FiMRNOZ5YuThqHkMCUT4f35/R1Ux13gFsNy7usYWA77MDqahNAjACpVWunJAOFOVI5eZ C6CRcvlfuV5yQzw8HMiqXAtFf5lrYoKVh3f7iy2ukGaX85ZLQxwAk= X-Received: by 2002:a17:90a:15d0:b0:365:46aa:e68c with SMTP id 98e67ed59e1d1-36546aaebffmr582175a91.15.1777828481171; Sun, 03 May 2026 10:14:41 -0700 (PDT) Received: from crewmobile-Dell-G15-5520.. ([2409:40d7:e:c40c:91b5:56f4:f96:9be6]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-364f4858842sm3177775a91.9.2026.05.03.10.14.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 May 2026 10:14:40 -0700 (PDT) From: Arnav Sharma To: ojeda@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Arnav Sharma Subject: [PATCH v3] rust: error: implement Display for Error Date: Sun, 3 May 2026 22:44:32 +0530 Message-ID: <20260503171432.7846-1-arnav4324@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Implement the fmt::Display trait for the kernel Error type. While the existing Debug implementation already outputs the error name (e.g., 'EPERM'), implementing Display allows errors to be formatted using the standard '{}' display marker. This provides a more idiomatic Rust API and allows Error to be used seamlessly with macros that expect the Display trait. Signed-off-by: Arnav Sharma --- Changes in v3: - Fixed doctest to prevent panics when CONFIG_SYMBOLIC_ERRNAME is disabled. - Corrected commit message regarding the output of the existing Debug implementation. - Retained `write!` for the fallback error case. Applying `f.pad()` to dynamically generated text requires memory allocation, which is unsafe in error formatting paths. rust/kernel/error.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 05cf869ac090..4ab7d7d8ccad 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -216,6 +216,28 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } +impl fmt::Display for Error { + /// Displays the error name if available, otherwise the numeric error code. + /// + /// # Examples + /// + /// ``` + /// # use kernel::prelude::*; + /// let err = EPERM; + /// // Formats to "EPERM" or "Unknown error 1" depending on CONFIG_SYMBOLIC_ERRNAME. + /// let _ = fmt!("{err}"); + /// ``` + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.name() { + // Print out the numeric error code if no name can be found. + None => write!(f, "Unknown error {}", -self.0.get()), + // SAFETY: These strings are ASCII-only. + Some(name) => f.pad(unsafe { + core::str::from_utf8_unchecked(name.to_bytes()) + }), + } + } +} impl From for Error { #[inline] fn from(_: AllocError) -> Error { -- 2.43.0