Received: by 2002:ab2:23c8:0:b0:1f2:fdbc:cb93 with SMTP id a8csp38374lqe; Tue, 26 Mar 2024 19:36:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVgcTRt/EAQL4RDSnFelTvXiAId0WpqYrL/XIcBtou6UNxLqqaWMz/b0kplI9MZ3ZXViphx/+YAvnhlyg6AHxr27Yc0M+KSsFJltsTStg== X-Google-Smtp-Source: AGHT+IH8g/QwJs5fQySumSAna1OueOotlgQWvJcWQOF9NrEc391DOTjSGwLzXsHDxhLJ5B49j0d/ X-Received: by 2002:a05:6000:dc6:b0:33e:d865:41f with SMTP id dw6-20020a0560000dc600b0033ed865041fmr2059102wrb.35.1711506992193; Tue, 26 Mar 2024 19:36:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711506992; cv=pass; d=google.com; s=arc-20160816; b=qyZgrc0QSUHMSly89FAiO9gAXFoCXriL06cU6mOutloHIZ5TgErOSMWU8eggDHl4BQ Bbw75DrK6yPkf4cFDuHPC1uTOkT+dACBa8Mm4PWOwdVkrpVDQ4tRMf282zkS9ixq8bMo oSIfDjIq+/Cpz74F50drh4oH5LwZRuhdBgl+lmfcGW6qEXhNXWam4x9XADoVdY2yBvJq ck4kCQVh7i/TqnpBbtfErUPgNTPctqrq9bT4bvsorsU2RxrLWDqBUxYQ+4uyl/vnQ9ao gA+v6af6X8N8WrFPkay/17KAxG9IdvixBWeZ9pa3XGCyGpixRNWm06aTf+sTAl8AJxrD gFxw== 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=is4/ykqJO1ReAP6DDSEum2VHbIujlrPWTW7BeyLRLts=; fh=jOs7SwCQAGiyC0TzUQb4Gl7OzQRTdbeTNJtp2uLqj60=; b=yfReCFy1REpcBd5yvltu4D8Mo1kHHdYdgOP+AS7QqANoncv+AxCCswMkftYhxiJH7E S3L7Xklf+L3PyPO3i70hf6jCVmDKce6cezykdCCCoP6266CmnSNsQOrj0Le0KC+bnO7I Em/3sTt/X2QC8aJ6UmmlMd9MmhWfRIO5pIcKq7DPSNWMiiKGUg89jrOaJcnCrPe0UK8a TVTLiobLA5KZoFsEp+wbBqikQ6yuJAoUTIkPuWi2g/ycItzwJTZKLOSseYP/euta5gO6 wJk8D29GHX9KoWehQV1OBo9SdZlQBhf2S0oPAi7EFjuNDoj/j4/TsDB20bDBpB5FJJJU XYsQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=R3bs4OtK; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-120224-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120224-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a3-20020a05640213c300b0056c28180884si1410850edx.689.2024.03.26.19.36.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 19:36:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-120224-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=R3bs4OtK; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-120224-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120224-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 BECA11F25C3D for ; Wed, 27 Mar 2024 02:36:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D2CAA22F00; Wed, 27 Mar 2024 02:36:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R3bs4OtK" Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) (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 7830722EFD; Wed, 27 Mar 2024 02:36:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711506961; cv=none; b=d8k7GQ5SBZ8Wx17dDR7DdpxTHxRl2Zg/GRXFM8uphJb8pfZc2/kV3hLGGQhndQeWcFqU+LxN7BzbGrpkQ23W1OYf9ABFjbobvoEJ2P5riq/QEX+cGLVNQz1g0WQY/k1hXZl0zFrgI0vawgNPmMPxFMQMFgJgXm0+TdI7hG4pVjg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711506961; c=relaxed/simple; bh=IilFXnlIHDAkkessE/SpSvQi2ra6lRcyKZ3Q/fq1hX0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V6Tu6qjfcNwBF6pUeF9zaJvr02s3prJFzNpXSFgrqixSy9oHEy8OaEE25Xb2Pgld7XgswfzyGmzSGtWEywIP//0DNaOE5MYt0vrWkSdysfGA9aHuYPnFSmGg/HbKAg2KLi2ufYcI6Jz/QwDtIQaSv26tDTIFaGXiP9oJ7TkPQPw= 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=R3bs4OtK; arc=none smtp.client-ip=209.85.215.170 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 Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-5bdbe2de25fso4350095a12.3; Tue, 26 Mar 2024 19:36:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711506960; x=1712111760; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=is4/ykqJO1ReAP6DDSEum2VHbIujlrPWTW7BeyLRLts=; b=R3bs4OtKpAtY8YI34qjEgzfPmLTEoNgygLf9r9OKRgphrUnHmrnjWiMV0t7NuWd5sw sfxMUHp5BUDWg42fguJYDgGUtS2xcQsk5aQgbBXf+WxHl1A+vBmr2fP+K8I7uvSMoZxl pOJG+d/honceI4D7P3ZAzHZmPrPTdxW9ERdTvHIT/aBccamIfTq/sDqwcWsRu6gbp9A3 boYHnQulv/ldPMlFatoQWVRXdcopEYTx8AXT2ad/blakLIWCCUErO15TPFcta0+oZIvu khKV1qTFw5SgNi/Ztp5Zp/mTx1os2iDYTJaTpnmSm9jh+hLXCq6A2FvJlGYFcMPylbkY XR9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711506960; x=1712111760; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=is4/ykqJO1ReAP6DDSEum2VHbIujlrPWTW7BeyLRLts=; b=StOTSKXeH+EtgvBgHRGEeJXIOoa3Cm379jLiA2PoUnMMED/paVSbzIcyf/zFvNWY88 qk1m35B6abY0s4j72fuOEEz+bjx7mkLrUrUguFHQflGJFUl7gS2YXzJrbVNID7UXwopK KMhLRu06MzSdeY8rcHZXGfyzES3cwNHXuTNWpq/rzttsy9bipn+hA6PcrLLYNxWmh/74 OcRhqpwyF4vd5uk3xT1HAfrAbJQ9zvUKiGcwq/BTtGZdvG49Ub5zdzB+0v3I49p+93eJ 1shsEGvkpUXWXTRCTsItjIsIEeisHeMcDjB8Djk0yJ85EU9ev6irlyMzo4aZc8iSbAq+ VJVg== X-Forwarded-Encrypted: i=1; AJvYcCW1lI16jvBU2/Bdg9XaD/w3ChULgO5M5LOkWbOiip+L3l9PGFy1PbrqNHYEb9ifTYLUFwkiN+kxacUxeFLbKysoBcmgQ5KkzoWljjok X-Gm-Message-State: AOJu0YytC13MKnX46d2/AF0TZ2xIBm5dc5T1jmvPcCJhG8R+xLIa3+/N XNHphgNHn2NLrXnnrFat50sWM3GNZay+oLcGoknlCe4pi8javnWbrXmkReOx X-Received: by 2002:a05:6a20:548c:b0:1a3:c4c2:b016 with SMTP id i12-20020a056a20548c00b001a3c4c2b016mr1645069pzk.45.1711506959786; Tue, 26 Mar 2024 19:35:59 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id l20-20020a17090a409400b0029b2e5bc1b9sm347441pjg.23.2024.03.26.19.35.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 19:35:59 -0700 (PDT) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [PATCH v2 02/10] rust: alloc: introduce the `VecExt` trait Date: Tue, 26 Mar 2024 23:35:23 -0300 Message-Id: <20240327023531.187880-3-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240327023531.187880-1-wedsonaf@gmail.com> References: <20240327023531.187880-1-wedsonaf@gmail.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 From: Wedson Almeida Filho Make `try_with_capacity`, `try_push`, and `try_extend_from_slice` methods available in `Vec` even though it doesn't implement them. It is implemented with `try_reserve` and `push_within_capacity`. This is in preparation for switching to the upstream `alloc` crate. Suggested-by: Gary Guo Signed-off-by: Wedson Almeida Filho --- rust/kernel/alloc.rs | 1 + rust/kernel/alloc/vec_ext.rs | 48 ++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 2 ++ 4 files changed, 52 insertions(+) create mode 100644 rust/kernel/alloc/vec_ext.rs diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 9aa5077d4a4c..2c99635f9fd3 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -5,3 +5,4 @@ #[cfg(not(test))] #[cfg(not(testlib))] mod allocator; +pub mod vec_ext; diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs new file mode 100644 index 000000000000..311e62cc5784 --- /dev/null +++ b/rust/kernel/alloc/vec_ext.rs @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Extensions to [`Vec`] for fallible allocations. + +use alloc::{collections::TryReserveError, vec::Vec}; +use core::result::Result; + +/// Extensions to [`Vec`]. +pub trait VecExt: Sized { + /// Creates a new [`Vec`] instance with at least the given capacity. + fn try_with_capacity(capacity: usize) -> Result; + + /// Appends an element to the back of the [`Vec`] instance. + fn try_push(&mut self, v: T) -> Result<(), TryReserveError>; + + /// Pushes clones of the elements of slice into the [`Vec`] instance. + fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), TryReserveError> + where + T: Clone; +} + +impl VecExt for Vec { + fn try_with_capacity(capacity: usize) -> Result { + let mut v = Vec::new(); + v.try_reserve(capacity)?; + Ok(v) + } + + fn try_push(&mut self, v: T) -> Result<(), TryReserveError> { + if let Err(retry) = self.push_within_capacity(v) { + self.try_reserve(1)?; + let _ = self.push_within_capacity(retry); + } + Ok(()) + } + + fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), TryReserveError> + where + T: Clone, + { + self.try_reserve(other.len())?; + for item in other { + self.try_push(item.clone())?; + } + + Ok(()) + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 51f30e55bd00..7f2841a18d05 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -19,6 +19,7 @@ #![feature(offset_of)] #![feature(receiver_trait)] #![feature(unsize)] +#![feature(vec_push_within_capacity)] // Ensure conditional compilation based on the kernel configuration works; // otherwise we may silently break things like initcall handling. diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index ae21600970b3..c85b5972c0d3 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -14,6 +14,8 @@ #[doc(no_inline)] pub use core::pin::Pin; +pub use crate::alloc::vec_ext::VecExt; + #[doc(no_inline)] pub use alloc::{boxed::Box, vec::Vec}; -- 2.34.1