From patchwork Thu Nov 10 16:41:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18214 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp251899wru; Thu, 10 Nov 2022 08:44:54 -0800 (PST) X-Google-Smtp-Source: AMsMyM4Y8fjPFcCcFNWTTi2O/eE+1FVnJhR6eb+k6/knSCBAu0IpVx8s2BpoDt6zNFBGIdQDO0hB X-Received: by 2002:a17:907:3e88:b0:78d:b3f0:b5c0 with SMTP id hs8-20020a1709073e8800b0078db3f0b5c0mr3253814ejc.141.1668098694154; Thu, 10 Nov 2022 08:44:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098694; cv=none; d=google.com; s=arc-20160816; b=qG770LrwxAGjZ9Iy99lipzZn4xnX4ZiMwPatSWl0CTfYlvMSGBNcrt8BxS+gOxzm38 4ZnZjft/bx7kwvuZ2ATxWtUI5JLTwbgbPwBc3TobJv0O7mxKle4ghc73EaquHN5r0zi3 VbbzcoNKfPtuaWjiMbsVAQ/8wTlEKD/DYdP84llEYvLsbvUfCeaGFpMpxMGsICj4Yd9S xLKnKWVETbel9vh5Kyt8z+VA5i28vdL2qK8z4t5LY/wVMvBIsc4HWnV0vGr646npJNe3 Si+tEzGxvFCa6QPC3DlLKy4gWEDcUmDMox2AKm0f5M/1/81Oxi7N65cbYH93r0yUZW1h 4SuA== 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=fWoofikj69iRbd4Mfwg7Z519A3E0rOj80lwqfIbn7js=; b=JaKH9LTrmbT10DmvYaHKtAClODYq6T4cTdgDNpKGhW6zbViDukegw217AEGDcEz8Vq dJeBz9VHymQhz5QAw+FD9OIGB4DypbLj2Gr9tdpLARmH0+IwHyed2rILvs0iWuTtqlhX ULXMEjTNNQCexENlpeJQuib+I9hDsiDN1CbjwWM8lzpDY9oqjawh2Xu/NJ9yMdmNvNA2 XRXtXz2K3pkWxuo8aGxB4pZ9p9GxLOpzSP+tDwqFw8C3l2m48Mb1UvBG4QmJPCh8FQfa Gsf0yAWympKTCZjzps0bw3JfbEU+bCa8j4548INL7Z1vWf44cU/ZphrnC89Y0658Oein +4Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=s791YN2U; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nd18-20020a170907629200b007ae417013desi21609407ejc.324.2022.11.10.08.44.30; Thu, 10 Nov 2022 08:44: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=s791YN2U; 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 S232363AbiKJQoB (ORCPT + 99 others); Thu, 10 Nov 2022 11:44:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232099AbiKJQn0 (ORCPT ); Thu, 10 Nov 2022 11:43:26 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A4294385A; Thu, 10 Nov 2022 08:43:00 -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 ams.source.kernel.org (Postfix) with ESMTPS id DA077B82251; Thu, 10 Nov 2022 16:42:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88272C433B5; Thu, 10 Nov 2022 16:42:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098577; bh=BvwkwmrbZC14+/ZpCX1HQjoc2T2Qpowc2X8TOjeQukE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s791YN2UzC8dhtmjVNGRnI0TlhTl/C5cNWs9GU+8N/CMDuw3sUQPrw+9/ee8KnDtq mMSuLzJmPNMiWBRTYbSSUnXLCQtLRoUuEzrO37LiXy6Z+M49S03NLaWfgpqAJ7dGgh dz2Acl6UXWkPAqNeWejXTcFL7NAtRPti3g/PN416jMnL3g1Rn9bUr1iNCNtNk75HvL US1JOiBnvbEJyP/Vb3XpgeL+fhv1QMd+DnvrRhSZK2lAZ9k5r+TB8pxuyMoaHQ2Woe Sm2g/YYENLlz4ojLxVqaov38JdbUKh3LWPonkNSR3EhKCJynHtCFy/KZXLarJN7zvE UL4DCRGqp3CFw== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v1 13/28] rust: alloc: add `Vec::try_with_capacity{,_in}()` constructors Date: Thu, 10 Nov 2022 17:41:25 +0100 Message-Id: <20221110164152.26136-14-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749128256266645713?= X-GMAIL-MSGID: =?utf-8?q?1749128256266645713?= Add `Vec::try_with_capacity()` and `Vec::try_with_capacity_in()` as the fallible versions of `Vec::with_capacity()` and `Vec::with_capacity_in()`, respectively. The implementations follow the originals and use the previously added `RawVec::try_with_capacity_in()`. In turn, `Vec::try_with_capacity()` will be used to implement the `CString` type (which wraps a `Vec`) in a later patch. Signed-off-by: Miguel Ojeda Reviewed-by: Gary Guo --- rust/alloc/raw_vec.rs | 1 - rust/alloc/vec/mod.rs | 89 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/rust/alloc/raw_vec.rs b/rust/alloc/raw_vec.rs index c342f3843972..eb77db5def55 100644 --- a/rust/alloc/raw_vec.rs +++ b/rust/alloc/raw_vec.rs @@ -135,7 +135,6 @@ impl RawVec { /// Like `try_with_capacity`, but parameterized over the choice of /// allocator for the returned `RawVec`. - #[allow(dead_code)] #[inline] pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) diff --git a/rust/alloc/vec/mod.rs b/rust/alloc/vec/mod.rs index 540787804cc2..8ac6c1e3b2a8 100644 --- a/rust/alloc/vec/mod.rs +++ b/rust/alloc/vec/mod.rs @@ -472,6 +472,48 @@ impl Vec { Self::with_capacity_in(capacity, Global) } + /// Tries to construct a new, empty `Vec` with the specified capacity. + /// + /// The vector will be able to hold exactly `capacity` elements without + /// reallocating. If `capacity` is 0, the vector will not allocate. + /// + /// It is important to note that although the returned vector has the + /// *capacity* specified, the vector will have a zero *length*. For an + /// explanation of the difference between length and capacity, see + /// *[Capacity and reallocation]*. + /// + /// [Capacity and reallocation]: #capacity-and-reallocation + /// + /// # Examples + /// + /// ``` + /// let mut vec = Vec::try_with_capacity(10).unwrap(); + /// + /// // The vector contains no items, even though it has capacity for more + /// assert_eq!(vec.len(), 0); + /// assert_eq!(vec.capacity(), 10); + /// + /// // These are all done without reallocating... + /// for i in 0..10 { + /// vec.push(i); + /// } + /// assert_eq!(vec.len(), 10); + /// assert_eq!(vec.capacity(), 10); + /// + /// // ...but this may make the vector reallocate + /// vec.push(11); + /// assert_eq!(vec.len(), 11); + /// assert!(vec.capacity() >= 11); + /// + /// let mut result = Vec::try_with_capacity(usize::MAX); + /// assert!(result.is_err()); + /// ``` + #[inline] + #[stable(feature = "kernel", since = "1.0.0")] + pub fn try_with_capacity(capacity: usize) -> Result { + Self::try_with_capacity_in(capacity, Global) + } + /// Creates a `Vec` directly from the raw components of another vector. /// /// # Safety @@ -617,6 +659,53 @@ impl Vec { Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 } } + /// Tries to construct a new, empty `Vec` with the specified capacity + /// with the provided allocator. + /// + /// The vector will be able to hold exactly `capacity` elements without + /// reallocating. If `capacity` is 0, the vector will not allocate. + /// + /// It is important to note that although the returned vector has the + /// *capacity* specified, the vector will have a zero *length*. For an + /// explanation of the difference between length and capacity, see + /// *[Capacity and reallocation]*. + /// + /// [Capacity and reallocation]: #capacity-and-reallocation + /// + /// # Examples + /// + /// ``` + /// #![feature(allocator_api)] + /// + /// use std::alloc::System; + /// + /// let mut vec = Vec::try_with_capacity_in(10, System).unwrap(); + /// + /// // The vector contains no items, even though it has capacity for more + /// assert_eq!(vec.len(), 0); + /// assert_eq!(vec.capacity(), 10); + /// + /// // These are all done without reallocating... + /// for i in 0..10 { + /// vec.push(i); + /// } + /// assert_eq!(vec.len(), 10); + /// assert_eq!(vec.capacity(), 10); + /// + /// // ...but this may make the vector reallocate + /// vec.push(11); + /// assert_eq!(vec.len(), 11); + /// assert!(vec.capacity() >= 11); + /// + /// let mut result = Vec::try_with_capacity_in(usize::MAX, System); + /// assert!(result.is_err()); + /// ``` + #[inline] + #[stable(feature = "kernel", since = "1.0.0")] + pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { + Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 }) + } + /// Creates a `Vec` directly from the raw components of another vector. /// /// # Safety