Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2918141lqp; Mon, 25 Mar 2024 13:03:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXGorUH/BqMph390+aihUazMU3SGH7u+15fNs6wEolKuAeW4FPvomOSzdD3pPvPr6/GYkt7EM6nhTCqrLGRbQfTm+irgZDdcUR657FHFA== X-Google-Smtp-Source: AGHT+IGwBd84kbF+GNIE8R2jiCbvCfhCskyIoAgqgRWFLR9b2T2H81HfnXfccy3RZ1HB4tWY29BW X-Received: by 2002:adf:e404:0:b0:33e:72be:43ab with SMTP id g4-20020adfe404000000b0033e72be43abmr6683294wrm.24.1711397002544; Mon, 25 Mar 2024 13:03:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711397002; cv=pass; d=google.com; s=arc-20160816; b=CdywpNzHJkm85aEouK1IxfHat6SHmfu0tI2oMkKzCriBa3YzvkU9JV9ImBsFYWyEK2 gIPg2Kwn4J4IScbRIxHvyPcB/oNUxmsuRsaj6uHAoOIgIltHYX40OYli9cG3PVNsQ8BW mcBxezR7cbZuS6cYOv39vFyyPxYcdYZycxr1UQlNUkeE6FGQn91qm9ZQdStbHAfXUqTJ r7w/TdGGdxibFVZvN4/6tX9rgguV9VZHJg+96o3yj1RxHFoUitDXRkBdejND5eXmh4xA q0+V2yf70ZSu2t4DM4uih9erQYXzFbCOH7rusK2LRJLNbZSPISRoc/x+f6ZDuizwY3B/ zY1Q== 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=AfAqoR8fGqIvWyJtLhGuGzpKHZ3UCvocy5kc8JQKQzo=; fh=L/xiXs7M5UcpLyQ6cbFyNBgix9bUsD5MQN7EuRWORw4=; b=fiRbGtKbXr1bLYLzRNDM7uD3OKe4LiTHTVuMGUXOgO1d4EWLTb1SjlvNr07bD7YRvx HxeCdYZpF1dthTavlGUpGtSIgb8K2WOHvrvVO0ndJBBHbT6TNqDnFvjivKChm7cOpGSY XCP0FLEPB8wT8BnLpPsrkR0vBQlkINv7f+ctHRKK1aWdSSUpH3nR7UXyPaQxy97quGr0 dAPQDcst1c5iIfmUZs8CLqItLYfig6AckLDdlSJQounhyBfxoHZFraxCgCDbHWIYSVHQ 7R/rm6X+69jrW2C080If6xB8FmHS3Har5k4Lw5B605rwqNN65x37VgOJmUYXID1ZHJKE 3lVw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="c5t/psbO"; 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-117863-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117863-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t24-20020a170906065800b00a4751901ed4si1770270ejb.118.2024.03.25.13.03.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 13:03:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-117863-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=@gmail.com header.s=20230601 header.b="c5t/psbO"; 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-117863-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117863-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 EBF351FA60E4 for ; Mon, 25 Mar 2024 19:56:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A09D52F81; Mon, 25 Mar 2024 19:55:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c5t/psbO" Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 E080B4AED8; Mon, 25 Mar 2024 19:54:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711396499; cv=none; b=uJ8foeTMHgrUoBk62G6QRzMdJGZ8P7ZlBYyu0Oz54hYN9YEdVlPwmnLUHS5vmKDRYcvZRU5zCatBBqjOVCs/GlvPd0QWrBJyTIDALy+0lmoa2hBleDYFni8SRn60TukwoUZruzTi3iIcLEATrP0aLsAbOuXxAMx6/oh4gj99IsI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711396499; c=relaxed/simple; bh=GpvTAhqfJsdWxTHJcyLn592rRU+7rJcIUsEbCsvcXt8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gvuVooZCAGSk8yQDKqZ7RYrWKEczedEvqJhajJgBCH0z8QJnbbQ/ZmYwwbMpeaGCjRbTgONW5Iluh9nTWytKw6NcuJ5DaOSbwOCfJvZvSrg1tRV3ThTMPQcWd7Lma69i+HlW1cfHZ1tQmYj17RAGPrXbAnRWrOcur96N31K/sgc= 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=c5t/psbO; arc=none smtp.client-ip=209.85.214.182 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-pl1-f182.google.com with SMTP id d9443c01a7336-1df01161b39so37137015ad.3; Mon, 25 Mar 2024 12:54:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711396497; x=1712001297; 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=AfAqoR8fGqIvWyJtLhGuGzpKHZ3UCvocy5kc8JQKQzo=; b=c5t/psbOuHKwEE7l63yWZC0MC7I2adp6DZKSN/hXZWpDBxa16CvJPdvgVKW4K1k4aE Gi4GF24KVg/KY57RMsaak0AqHLK+/aXgfea7VCcprRqRwHwsrjO2uL8XOWvQYeu+Y8tM z9APACrV8fGZelRFiS0nUyiQ7gG2RFwisJ9LQIjvBky9F/ychpnTsvMk1ezbAKs0O4/I iKRTxKpw4QPs8RX16uJIGq6G+utYt96gXRzkejVz84x0bxGjv64arAdoySw5OLGrY4da +N8pA6Pa85DKUSdF3SJ6bNPW15Ycqn3rTLMm9AgWvOztp+Zg+nOi0egvk6s6JTevNzE8 SZ0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711396497; x=1712001297; 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=AfAqoR8fGqIvWyJtLhGuGzpKHZ3UCvocy5kc8JQKQzo=; b=b3LICB9HBh0mIXDYhE1XoF6iUUyOhDM00ky+NiZ+uP5OIfjq0AHRcSfSDKrwMbI1Zv mMAV+EL/gBaLOggyFjDLAe3b/1dgwR0JNn42ty4aJWjzCIwfFbPtIQdhF3ZVubl/BvQd GcozZwzX+673P1JUBqLkFvui5nBN+SoRFcc5/kZFNWGiyosL2X4ScQjcjQF7C8brCnWD 4WwCaIFZb6wzRnOroIX5smv9Mb9mXsNuB+O3YTXNup88/JbpvfQXr6GLZKdbKzfMFHcD QsiFdojJCzQEChFLAJnALxJDpD4NAq2LFtJgbuTp75ISez3M8iHQ4Nhmxt5uGdWQ9giz ljUA== X-Forwarded-Encrypted: i=1; AJvYcCXgLgrZyKtqqbmQD09SAg7cv/OTMH20v67CNed6PRwTTxkg/YTuacBVIZxTnc9Wpt9++cOeYKBzcSrFHpBFxhrEfq3eZ4SR1CYfj95t X-Gm-Message-State: AOJu0Yzz+O9R5y4Zii2PNTtIjr8DXIF+54mK/E0ChFHdte6FKsR1vmRv c5m8mjnnBaiZ0i+Ck1ZUGlkzbiaCDMRm9nX033zF2dK5TCevGAVFwy9lWrKz X-Received: by 2002:a17:902:f70e:b0:1e0:e56e:eddd with SMTP id h14-20020a170902f70e00b001e0e56eedddmr451549plo.32.1711396497016; Mon, 25 Mar 2024 12:54:57 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id q1-20020a170902dac100b001e06c1eee22sm5099585plx.74.2024.03.25.12.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 12:54:56 -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 02/10] rust: alloc: introduce the `VecExt` trait Date: Mon, 25 Mar 2024 16:54:10 -0300 Message-Id: <20240325195418.166013-3-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240325195418.166013-1-wedsonaf@gmail.com> References: <20240325195418.166013-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/vecext.rs | 52 +++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/prelude.rs | 2 ++ 4 files changed, 56 insertions(+) create mode 100644 rust/kernel/alloc/vecext.rs diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index ccd4149932c3..8ad57a2e693e 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 vecext; diff --git a/rust/kernel/alloc/vecext.rs b/rust/kernel/alloc/vecext.rs new file mode 100644 index 000000000000..59e92bab534e --- /dev/null +++ b/rust/kernel/alloc/vecext.rs @@ -0,0 +1,52 @@ +// 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, + { + let extra_cap = self.capacity() - self.len(); + if extra_cap > 0 { + self.try_reserve(extra_cap)?; + } + + 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..a0177f195dec 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::vecext::VecExt; + #[doc(no_inline)] pub use alloc::{boxed::Box, vec::Vec}; -- 2.34.1