From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 6E22229DB6E for ; Sun, 3 May 2026 15:17:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777821441; cv=none; b=VYcIUmOsVa3rNzYyti4UbRu+FjqXuLOw4Lx0rqPyVC5WoyoHyGgr1HTglo7Nm9J2iLVMuH6vruh3UH35NbuftBQLEOVGR8uhtA2TIfPjS7TjEKNXqT+8seJo3NQf8CFq4EtiKnNuZ9AQeyeSLR1YCPj8+w1qrm8dvM82nSK7Fmk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777821441; c=relaxed/simple; bh=LZI0jQq1N8UGTjd1BjSQ4WvOjNUHUalOZeaxkEfH9VA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=QAaHSnNZvLXt5nQ4xXwTCI1qDG16gvfxaxfOPQbwMCd2gB8Xp1y+usMPTjygx+K05Czz5dVWRA6cD047oe+j0hvuSNB8bVjMPO2qmPzMar8ZXQNcQX4QOPJiRymhO+dbTNjfhKcuXoGzSmN+Alb8yP84UDpq+qZvxRpx9Ch+b6I= 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=fc1x9WR1; arc=none smtp.client-ip=209.85.210.180 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="fc1x9WR1" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-83538fbd0b2so306276b3a.0 for ; Sun, 03 May 2026 08:17:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777821438; x=1778426238; 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=ETXYuNQamoYCjCGsQiZmhWWNm8oXqGJiz2bukvEMo8Q=; b=fc1x9WR1uvZPRX+EyvU46LNHyfID//3xvVnzc4vTbun4kKtSud1UJUqWnByYiajum5 aUI8R/sX6OUHF5nc/YF6WdCsVkmv8Vsht9NDu2mmTajFM/Dpzc3x1aaQFiW0u3R8o7Dl T+CRfqBytndv7YbE1xmwzf9Z60GTuAzilGQG3RFaLj7K4JZF3Y3Gfx9mZWNqG8SQRsOI 0YJj5FSmoMrmVu+BxGzXFEgjhnQrnjIO427uU3o6EWAnyp28sTJrxDMu5PvuZP9NvUBX emCn44gzISe7YYQ8XZhg8sjBtl2ucTkY3KVc6Tj4okjZ+UCQJiCsZvA4I8LGJDn8feI/ xVug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777821438; x=1778426238; 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=ETXYuNQamoYCjCGsQiZmhWWNm8oXqGJiz2bukvEMo8Q=; b=hQYV7NjHKoCPEYEqss99g+XOcaDHKcyPr8Sg2N7B16sddhqqsaMeJfiqAlZBeFOs++ DenS3n0PNlPfGWndEqAZteIHgIoHFozxfvdGG39NYvn792Y5NpHKOmOJqyk64VFPZW9U QSevXbj5JVtMeKxaYYbPb6FIWat5thNmx31wWfPk0kfi/X9IZwnCs1olqHzoE7WldPqX OfScJd4CUKPif7FfsSfNqgC3fU+3Nl+kqZ9e4Rbdq0phCB5YbQIgdG2Epw/BJtpOD5qX 7Ff6/8q5oyqjhhuVwdXVj4v9jOht3iPPZF9R8RJhKlva6uxSn05by26wKgWMuQGOFO4E 6+CA== X-Forwarded-Encrypted: i=1; AFNElJ/neqYmal8AaHriH2NGKaEegGGC1pZ0ql2SYIj7IMqP8A31RtHi7kHgiRQVCcLK0lN2wTR1z3Ux5ruW38I=@vger.kernel.org X-Gm-Message-State: AOJu0YyFR5cAi83NAVjZ3mjbrYwv7IACWXAytY193bSwYdRMG/R8FbPF PDY61iLdkNbCkx7pYeZmdVhujmR03wnTa2d0WJp5hC3cQuCgt1NJfSS/h8eYI3Cu X-Gm-Gg: AeBDieuziX9PhiZOjaH3C4PEohIknUzgVnaP9/0luL9PQRmvOE/qUqruG9EbZ1nKcuZ P0xI+RV6G/dD73ghRuPkEg9a2mISrQoT/qGa6teLMMtIYWltgUDFXt/SCsgWhyAUFzCTecXEGMO GrqwSsNKkaDZtz91RtTCm86IpGk/HUgefV6U4jqWe3DnSebptDltAucWHAw7rIavqPfLXq1Hb/G jOYNGll0+8d941hGLUzlcIATN1VEx/pC1hzFpGQhLCFp3EZnIWQUBI40YmseJo0nm+kZ4XJeKHn S6x3FTHUfV6/LpprxEuAwi/C0eX46ebLcBHxMy0nCAH1NjcTDrDTooekeCGvQfTGL2SItkbc9MO Ab6EPTbSnuPsCOokobQsYR/5AfqNpAsRXVC60pLyL9/xIJQHoTaH2cXrE2qVT4v8BbIGBqGBus3 NmFFeX04CmpTfn/yRFqcui6peKtGdFFyWkGA4A9S3lSTpAYtQ55SkJ X-Received: by 2002:a05:6a21:4ec4:20b0:3a2:d68d:9e78 with SMTP id adf61e73a8af0-3a7f1a31ec4mr4576938637.4.1777821437738; Sun, 03 May 2026 08:17:17 -0700 (PDT) Received: from crewmobile-Dell-G15-5520.. ([2409:40d7:e:c40c:9413:b194:9c5e:9abd]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7ffbb05798sm7174101a12.0.2026.05.03.08.17.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 May 2026 08:17:17 -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] rust: error: implement Display for Error Date: Sun, 3 May 2026 20:47:08 +0530 Message-ID: <20260503151708.26199-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. Unlike the existing Debug implementation which outputs 'EPERM()', Display produces cleaner user-facing output like 'EPERM', which is the idiomatic Rust way to present errors. This enables using {e} instead of {e:?} in format strings throughout the kernel's Rust code. Signed-off-by: Arnav Sharma --- rust/kernel/error.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 05cf869ac090..87ea3ce2d269 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -216,6 +216,31 @@ 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::*; + /// # use kernel::str::CString; + /// let err = EPERM; + /// let s = CString::try_from_fmt(fmt!("{err}"))?; + /// assert_eq!(s.to_bytes(), "EPERM".as_bytes()); + /// # Ok::<(), Error>(()) + /// ``` + 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.write_str(unsafe { + core::str::from_utf8_unchecked(name.to_bytes()) + }), + } + } +} + impl From for Error { #[inline] fn from(_: AllocError) -> Error { -- 2.43.0