Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp507233lqo; Thu, 16 May 2024 12:32:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXSoaYbvAjUhVW9nHxWkNSoTCPSX0WBuwX4g04e/gNdcbazuKXOWlT3rRUWWVfZqMgB1DGdlP/7tV7gaj7Cmj3pzueM4fw3eESp7heYow== X-Google-Smtp-Source: AGHT+IELtrrgAU0W7QGBXiGqFsbqaogTejN4wojhi8UqDXB6qRrg2fDKm2J+jQbXeGPjEH1KbEHi X-Received: by 2002:a50:8744:0:b0:575:a7e:4f82 with SMTP id 4fb4d7f45d1cf-5750a7e52e6mr2200538a12.4.1715887934016; Thu, 16 May 2024 12:32:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715887934; cv=pass; d=google.com; s=arc-20160816; b=CwiWceoZBgGMp060LNop17yWmqAgm3rr1va4dZXpBCKl0JzDOaOXAEtir4XNEZNyv5 nq8h0AUPaF2CXV+Esw3nz7GXdlPU4uWXzXhGf1AAX6VQOxS4u/QemAbIiZCKtpiZCJ8F E0LwPAIPm3e1X/BBt5ClfVdHmUrnAKyJ5AzaArvno+uzDFdKaeYvrXr2KhCwJe5xf48E 9YY0TyfiGqJpwIgsA/ZzcJxX1fZZ9HNHwejFGJi/VEIS4Qb08lkbYWRmpvrND0aeOB7Y mKukuBYrfWoEjNByW0bvZHsJUuJ/ULviBXr/pq8XDyHk5FyIq+IjxMhaznLRLgcedQK9 lxdA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=XmrkWfcmaLB4LqRKL0XJRG+hfTU9BH5HMiJRTIlB8AY=; fh=DGE/Rt7UZyKJ8ICVIgmeDLsrdvw5JULr/fcEfbQDVFQ=; b=Dui1YfqWzL8F6TN9iLGAWYKjcoKK1hvs3npx8RyrLNrZT9sM7bMR3Z9GMXh3ogNDRb TkOClA9sUIYkz/3da0yH4/knXP8/+82T6CJjC74k6+yQvnGXXTF1nwbQq1lYP3hu3OtT ElA1NQYO8dcfyHvFiTcM+MIJYKxTp9uSMqvwMqkazfAvi2wYJZVXf6i6psQ8zXzefxA7 8SYZujeXwdULH6ZjI5wc3T2JGkbyHzHHrnvQMKcu+ZcrDKpf+ax5i4rMtIW2VZ8IXf/5 Q9vsTT42rEPmPZiHLkuVaRl1pJ7cQ1BRQX14t13jMYoOW1kU+dzOxoFmsFGhxhDV9jxP GYYQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=Gl9srvSx; arc=pass (i=1 spf=pass spfdomain=cisco.com dkim=pass dkdomain=cisco.com dmarc=pass fromdomain=cisco.com); spf=pass (google.com: domain of linux-kernel+bounces-181505-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181505-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cisco.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-574e3a11b50si3685446a12.402.2024.05.16.12.32.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 12:32:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181505-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=Gl9srvSx; arc=pass (i=1 spf=pass spfdomain=cisco.com dkim=pass dkdomain=cisco.com dmarc=pass fromdomain=cisco.com); spf=pass (google.com: domain of linux-kernel+bounces-181505-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181505-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cisco.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 7FAE11F24084 for ; Thu, 16 May 2024 19:08:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4633715B99E; Thu, 16 May 2024 19:05:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=cisco.com header.i=@cisco.com header.b="Gl9srvSx" Received: from aer-iport-4.cisco.com (aer-iport-4.cisco.com [173.38.203.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F27E15ADA6; Thu, 16 May 2024 19:05:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.38.203.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715886317; cv=none; b=p7Q0pJ6emCfOIdE7NQhtqEHlAfax+GyqYmdfTg0mUm/01NhBoTzcbtoMAP1zmPv5jiEg/HjLdaABeb0PlfWPoXIyCFJBgurBPYYmIPjXx0tRlVQpJ3oUoRe7ZJd6AGgW9ReRNWtdkxXc45UyAWozZLotEmPnjrscS2XymKhNkNg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715886317; c=relaxed/simple; bh=C0ihuL2jj8QfBcPXgxKcqRDlDBwSCbXEYWR2OmjZkwU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ULd+DVRtz3GHpfi4WHKpvOJnC2ypfRxQykPxzrusejolyBD9CRc+LzxrL+fPPhzJp+3mGA04OMMQshJYXs1TTb3OelpcL+7qneaktXXJnug0W+oOmmvKUjYrKxh9zR00LXjnkyiaZRZxf9PLo4dvF5pShrtb/cE9Ytp53nBHqao= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cisco.com; spf=pass smtp.mailfrom=cisco.com; dkim=pass (1024-bit key) header.d=cisco.com header.i=@cisco.com header.b=Gl9srvSx; arc=none smtp.client-ip=173.38.203.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cisco.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cisco.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cisco.com; i=@cisco.com; l=4299; q=dns/txt; s=iport; t=1715886315; x=1717095915; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XmrkWfcmaLB4LqRKL0XJRG+hfTU9BH5HMiJRTIlB8AY=; b=Gl9srvSxVW0jkbRvkgSKPc4mXARCAYT5/MuxUjZGXwfPUH1uArzQdsgh Voyzqs3MgqjVAH2aWIAXWRqxLeboT4Bk/+ltKb359DbINvpc3+wvHZPHq GQ0yVjbtpw9/FoqZnOtu1v6ym/ub6PGOaaBetxU6Wfv0MAGzguSrp4o+l I=; X-CSE-ConnectionGUID: 4EVdI9oZSiGwXuoPzTiTfQ== X-CSE-MsgGUID: fSSpwu+cRWaQ6JdKcx1PCg== X-IronPort-AV: E=Sophos;i="6.08,165,1712620800"; d="scan'208";a="12379869" Received: from aer-iport-nat.cisco.com (HELO aer-core-8.cisco.com) ([173.38.203.22]) by aer-iport-4.cisco.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2024 19:04:04 +0000 Received: from localhost (ams3-vpn-dhcp4879.cisco.com [10.61.83.14]) (authenticated bits=0) by aer-core-8.cisco.com (8.15.2/8.15.2) with ESMTPSA id 44GJ43g0042440 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 16 May 2024 19:04:03 GMT From: Ariel Miculas To: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, tycho@tycho.pizza, brauner@kernel.org, viro@zeniv.linux.org.uk, ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, shallyn@cisco.com, Ariel Miculas Subject: [RFC PATCH v3 05/22] rust: hex: add encode_hex_iter and encode_hex_upper_iter methods Date: Thu, 16 May 2024 22:03:28 +0300 Message-Id: <20240516190345.957477-6-amiculas@cisco.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240516190345.957477-1-amiculas@cisco.com> References: <20240516190345.957477-1-amiculas@cisco.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Authenticated-User: amiculas@cisco.com X-Outbound-SMTP-Client: 10.61.83.14, ams3-vpn-dhcp4879.cisco.com X-Outbound-Node: aer-core-8.cisco.com Add encode_hex_iter and encode_hex_upper_iter which return an iterator, so it can be used in conjunction with the Vec::from_iter_fallible function to (try to) create a hex string. Replace BytesToHexChars with BytesToHexByteSequence because String is not available. We need to collect the values in a Vec, append a NUL character and the create a CStr on top of it. Also remove the ToHex trait because it uses the FromIterator trait which cannot be implemented in the kernel due to the prohibition of infallible allocations. Signed-off-by: Ariel Miculas --- rust/hex/lib.rs | 60 ++++++++++++------------------------------------- 1 file changed, 14 insertions(+), 46 deletions(-) diff --git a/rust/hex/lib.rs b/rust/hex/lib.rs index a23def3f80db..3abf61ccbfc3 100644 --- a/rust/hex/lib.rs +++ b/rust/hex/lib.rs @@ -57,43 +57,18 @@ #[cfg(all(feature = "alloc", feature = "serde"))] pub use crate::serde::{serialize, serialize_upper}; -/// Encoding values as hex string. -/// -/// This trait is implemented for all `T` which implement `AsRef<[u8]>`. This -/// includes `String`, `str`, `Vec` and `[u8]`. -/// -/// # Example -/// -/// ``` -/// use hex::ToHex; -/// -/// println!("{}", "Hello world!".encode_hex::()); -/// # assert_eq!("Hello world!".encode_hex::(), "48656c6c6f20776f726c6421".to_string()); -/// ``` -/// -/// *Note*: instead of using this trait, you might want to use [`encode()`]. -pub trait ToHex { - /// Encode the hex strict representing `self` into the result. Lower case - /// letters are used (e.g. `f9b4ca`) - fn encode_hex>(&self) -> T; - - /// Encode the hex strict representing `self` into the result. Upper case - /// letters are used (e.g. `F9B4CA`) - fn encode_hex_upper>(&self) -> T; -} - const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef"; const HEX_CHARS_UPPER: &[u8; 16] = b"0123456789ABCDEF"; -struct BytesToHexChars<'a> { +struct BytesToHexByteSequence<'a> { inner: ::core::slice::Iter<'a, u8>, table: &'static [u8; 16], - next: Option, + next: Option, } -impl<'a> BytesToHexChars<'a> { - fn new(inner: &'a [u8], table: &'static [u8; 16]) -> BytesToHexChars<'a> { - BytesToHexChars { +impl<'a> BytesToHexByteSequence<'a> { + fn new(inner: &'a [u8], table: &'static [u8; 16]) -> BytesToHexByteSequence<'a> { + BytesToHexByteSequence { inner: inner.iter(), table, next: None, @@ -101,15 +76,15 @@ impl<'a> BytesToHexChars<'a> { } } -impl<'a> Iterator for BytesToHexChars<'a> { - type Item = char; +impl<'a> Iterator for BytesToHexByteSequence<'a> { + type Item = u8; fn next(&mut self) -> Option { match self.next.take() { Some(current) => Some(current), None => self.inner.next().map(|byte| { - let current = self.table[(byte >> 4) as usize] as char; - self.next = Some(self.table[(byte & 0x0F) as usize] as char); + let current = self.table[(byte >> 4) as usize]; + self.next = Some(self.table[(byte & 0x0F) as usize]); current }), } @@ -121,7 +96,7 @@ fn size_hint(&self) -> (usize, Option) { } } -impl<'a> iter::ExactSizeIterator for BytesToHexChars<'a> { +impl<'a> iter::ExactSizeIterator for BytesToHexByteSequence<'a> { fn len(&self) -> usize { let mut length = self.inner.len() * 2; if self.next.is_some() { @@ -131,19 +106,12 @@ fn len(&self) -> usize { } } -#[inline] -fn encode_to_iter>(table: &'static [u8; 16], source: &[u8]) -> T { - BytesToHexChars::new(source, table).collect() +pub fn encode_hex_iter<'a>(source: &'a [u8]) -> impl iter::Iterator + 'a { + BytesToHexByteSequence::new(source, HEX_CHARS_LOWER).into_iter() } -impl> ToHex for T { - fn encode_hex>(&self) -> U { - encode_to_iter(HEX_CHARS_LOWER, self.as_ref()) - } - - fn encode_hex_upper>(&self) -> U { - encode_to_iter(HEX_CHARS_UPPER, self.as_ref()) - } +pub fn encode_hex_upper_iter<'a>(source: &'a [u8]) -> impl iter::Iterator + 'a { + BytesToHexByteSequence::new(source, HEX_CHARS_UPPER).into_iter() } /// Types that can be decoded from a hex string. -- 2.34.1