Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp943437rwb; Thu, 10 Nov 2022 09:10:54 -0800 (PST) X-Google-Smtp-Source: AMsMyM5UstZ3xFnGtZ/wGz4L+2hbGPxTe8hUCNAxv7Sumrrwvzgs4K37ANCdGZAZY91ycxf3bFOY X-Received: by 2002:a17:90b:1d09:b0:213:ff80:17fd with SMTP id on9-20020a17090b1d0900b00213ff8017fdmr51908086pjb.14.1668100254244; Thu, 10 Nov 2022 09:10:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668100254; cv=none; d=google.com; s=arc-20160816; b=OOc7zpcdWhmZ+fxtaIcuSSI2vH6GhiAIHSrytTlmgBQAA8/2ffPJDPNpQG90VmdJd1 8e3WQvxrhnHDDGb/Gi3l6NVGdxARJHZdy/usXvpd1S1JUz2bF/8NyBFKilD/Gl9ELUVF u32mE+8280Yb7Ti5Dct+yYWlSzjz3RIjK0x17emVDC0RXYtG/D0wxtM9ckloPDXpyUb4 xdC90McWXavSAjI4DTod5mfX1TNagDuAB6/v5dOxmn3cfqjrDNUa+1Lvd4aXjqm33O9x exKr7VTY//V/ZSD8Qgcc6z+m5YVMtFwy4KUF6mvLQ2bcRj41o2wDZ58OQPeTlxZcDU9P N+6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IqCDQzIn8PmU18Zo/aENvJsgJezqjD8PzkMlbpxej+E=; b=En31/ZXU9gfC1Z2+2yeXjcgAcL8kyv1LkvNdpgRL05hg9B9xJGSHbrnoVso+ff6vPY P0Z3vTIGLmoJhrqgRU4MXvcRjrrKgwvkcLOeJhAtcSWGKk2wn6CW8n7577YNhTpH/oYq YE9ti15rQ6uKWAfqioUIA7sXwXKEMNuMSqrtiLCtWFV+yfuDxBLHz5Fau3+Vg3JwxZnp T55KcWXRL/hdjbQjnzV9MA+6tY/7ZeNcfz69Hgo/exiV4XsURL9m7XSWQOhXXjVXpU85 WSPf20mSYOQtgzbJ3W2egE/shwhBfcicmVR9LmvbkWK6fyzmqJP1BMaeh4MUhJxSje+/ DV6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WXfn05iC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a16-20020a170902b59000b00186bb2e85c4si18127236pls.429.2022.11.10.09.10.35; Thu, 10 Nov 2022 09:10:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WXfn05iC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232507AbiKJQoQ (ORCPT + 92 others); Thu, 10 Nov 2022 11:44:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232229AbiKJQnb (ORCPT ); Thu, 10 Nov 2022 11:43:31 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 518064384D; Thu, 10 Nov 2022 08:43:10 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E2D8560C3C; Thu, 10 Nov 2022 16:43:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 20A19C433C1; Thu, 10 Nov 2022 16:43:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098589; bh=lSrHt+F2Ze+/0shcXyGqT2iYHkhHUzJS18BpgwZFKfQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WXfn05iCKkaThQThCXeKWEliWM8sYgdfDebypjfYb7zVgGuFoYbQC0a9EXewcfKeM u1FwVCtDj7pABLT8E+A9RmwU1fg1dCOtsxDfD/9PVLaCScNel7NS0s/0/wfJeHXF/1 KMaM7KmBO4fwh0PCgDYgJCLG+qPhmpBACcfnPEQc4ndGdA29MBi4FYx6t3rDHhV9ba utPqEiLMNvDmlJvS2KIyAZwP8FJzQJofuk1yjJYo7u9NL44ikmQQnLSV7ipciu9EwX GrF7h4ljSCT/DwahFTNF1UfB+o4pblBGXx5RLhD27EzfsAnLsiFrmfkX/NkN2bGCgS 3usHAuTFJ3/wg== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Morgan Bartlett Subject: [PATCH v1 17/28] rust: str: implement several traits for `CStr` Date: Thu, 10 Nov 2022 17:41:29 +0100 Message-Id: <20221110164152.26136-18-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Gary Guo Implement `Debug`, `Display`, `Deref` (into `BStr`), `AsRef` and a set of `Index<...>` traits. This makes it `CStr` more convenient to use (and closer to `str`). Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Morgan Bartlett Signed-off-by: Morgan Bartlett Signed-off-by: Gary Guo [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda --- rust/kernel/str.rs | 124 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index d66565f92f71..11d297c1a61c 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -2,7 +2,8 @@ //! String representations. -use core::fmt; +use core::fmt::{self, Write}; +use core::ops::{self, Deref, Index}; use crate::{ bindings, @@ -199,6 +200,127 @@ impl CStr { } } +impl fmt::Display for CStr { + /// Formats printable ASCII characters, escaping the rest. + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::str::CStr; + /// # use kernel::str::CString; + /// let penguin = c_str!("????"); + /// let s = CString::try_from_fmt(fmt!("{}", penguin)).unwrap(); + /// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes()); + /// + /// let ascii = c_str!("so \"cool\""); + /// let s = CString::try_from_fmt(fmt!("{}", ascii)).unwrap(); + /// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes()); + /// ``` + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for &c in self.as_bytes() { + if (0x20..0x7f).contains(&c) { + // Printable character. + f.write_char(c as char)?; + } else { + write!(f, "\\x{:02x}", c)?; + } + } + Ok(()) + } +} + +impl fmt::Debug for CStr { + /// Formats printable ASCII characters with a double quote on either end, escaping the rest. + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::str::CStr; + /// # use kernel::str::CString; + /// let penguin = c_str!("????"); + /// let s = CString::try_from_fmt(fmt!("{:?}", penguin)).unwrap(); + /// assert_eq!(s.as_bytes_with_nul(), "\"\\xf0\\x9f\\x90\\xa7\"\0".as_bytes()); + /// + /// // Embedded double quotes are escaped. + /// let ascii = c_str!("so \"cool\""); + /// let s = CString::try_from_fmt(fmt!("{:?}", ascii)).unwrap(); + /// assert_eq!(s.as_bytes_with_nul(), "\"so \\\"cool\\\"\"\0".as_bytes()); + /// ``` + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("\"")?; + for &c in self.as_bytes() { + match c { + // Printable characters. + b'\"' => f.write_str("\\\"")?, + 0x20..=0x7e => f.write_char(c as char)?, + _ => write!(f, "\\x{:02x}", c)?, + } + } + f.write_str("\"") + } +} + +impl AsRef for CStr { + #[inline] + fn as_ref(&self) -> &BStr { + self.as_bytes() + } +} + +impl Deref for CStr { + type Target = BStr; + + #[inline] + fn deref(&self) -> &Self::Target { + self.as_bytes() + } +} + +impl Index> for CStr { + type Output = CStr; + + #[inline] + fn index(&self, index: ops::RangeFrom) -> &Self::Output { + // Delegate bounds checking to slice. + // Assign to _ to mute clippy's unnecessary operation warning. + let _ = &self.as_bytes()[index.start..]; + // SAFETY: We just checked the bounds. + unsafe { Self::from_bytes_with_nul_unchecked(&self.0[index.start..]) } + } +} + +impl Index for CStr { + type Output = CStr; + + #[inline] + fn index(&self, _index: ops::RangeFull) -> &Self::Output { + self + } +} + +mod private { + use core::ops; + + // Marker trait for index types that can be forward to `BStr`. + pub trait CStrIndex {} + + impl CStrIndex for usize {} + impl CStrIndex for ops::Range {} + impl CStrIndex for ops::RangeInclusive {} + impl CStrIndex for ops::RangeToInclusive {} +} + +impl Index for CStr +where + Idx: private::CStrIndex, + BStr: Index, +{ + type Output = >::Output; + + #[inline] + fn index(&self, index: Idx) -> &Self::Output { + &self.as_bytes()[index] + } +} + /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// It does not fail if callers write past the end of the buffer so that they can calculate the -- 2.38.1