Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp13570455rwd; Sat, 24 Jun 2023 02:43:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7f+KGipjFs9PQrFOzx5xibK3C7ZVVGBJcHI8f5rUn/mtphJVin4XIQE9pdEF9D88oB3AyH X-Received: by 2002:a05:6a20:8411:b0:10e:d134:d686 with SMTP id c17-20020a056a20841100b0010ed134d686mr30062394pzd.6.1687599822672; Sat, 24 Jun 2023 02:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687599822; cv=none; d=google.com; s=arc-20160816; b=ivgAdWSQ9gnmuyZ+2k2j4znKMSX/u4PNkdjkUPaWpWHqdJlF03/417enoMHvfJF6fx UST5dxFEujLcPj1MWh2PTHWlRetWYm3iC78DT8eJFwOiEmrCNm0aP54/L2R5D4OmU5R4 K7PzoXsv2QCtEYZdXxDVJ5qVCjw+tGLupwY5I6EQVs7ogEMXunfvR952VyuhBpEP0plM SZgs6dnXtFrRl11CyErHuyHKxqPTv48pX3RItjjgGZuKo7pIVVyZkymWJVIPnVmGthrQ fXk+QJGi/V7+HsKG/xqZfxC18lSJ2PnE80dLKjM+SXvnBY6tZEj/29R6mhgGZMFT13rZ UpUQ== 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 :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=gLSKxHqYjJDu1EPRJrKPDpLlPpYDJtCGZbauNM8TqoI=; fh=Y3hGqDvLhFUfCxW08frpp1lth+cS6Xq25ZUxZll0Zv0=; b=eXEg9IQzl1oanOwbFXYwkM/dyjS/LrrbHDsynedhsg1gtkl8mGj2T4DzVZ7TEtYF0A 0nQvdNrQp4ke2tkl692oE7xnLhFSvbWs69WV40L2bCxDTz5difK8Slbci8UsS9bEwzWD 9PGX8rycYYHWMi/TJ8SqGHVw1U5I2oHQnV6NEey3Xg7UPNs6mdQ3wjwnD1WB+FGQk551 PbPHT6LLFaMuop3fOE25rwvU8JmRk4rrwbTpryohbVPJGtrqPDw1Ksw7Xm7lWd6mKa6+ dmVur+oObWRXYtt8zMrtPrUgc75G6oUTzojM2qssZfMsqtRMM9ELsutEkoepArjWKfCp hSUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=PzF6EJxG; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u6-20020a63ef06000000b0053fb64b2590si1213047pgh.163.2023.06.24.02.43.29; Sat, 24 Jun 2023 02:43:42 -0700 (PDT) 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=@proton.me header.s=protonmail header.b=PzF6EJxG; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233052AbjFXJ0S (ORCPT + 99 others); Sat, 24 Jun 2023 05:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233090AbjFXJ0H (ORCPT ); Sat, 24 Jun 2023 05:26:07 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7286A2726 for ; Sat, 24 Jun 2023 02:25:41 -0700 (PDT) Date: Sat, 24 Jun 2023 09:25:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1687598739; x=1687857939; bh=gLSKxHqYjJDu1EPRJrKPDpLlPpYDJtCGZbauNM8TqoI=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=PzF6EJxG1JQsX9+/c2QvRBGF5Cbkpnue8an6zQc9ZiqUYDUbqRR6uVPFTdYoxa/Bm XHmJxP2+dwnwyHf3xCVMBvycPXyKDfrpaN8Bj5tmkrE7wWjqNdcgT0DqgpmPB6vDEW OGo9gZ1pYvS64q4AwSnH2I155EI8c4TqhNeZjHkl+FjUKDJtx/S5blWgJzPDYobl3p kcKJ6g3/OTVZOVXUBEFG84CoFPxfj3wPjJYttS6dDdVYjlT29F0Ggd9yxgSjmoIx7G s37c8geiKJZtxmImSKsQ7OZ8+PKwY5V37EkLz4u2s8DNqh6gwCSt8tlyZ/9iSIZknp dohrYK9kH30KA== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Asahi Lina Subject: [PATCH 6/7] rust: init: Add functions to create array initializers Message-ID: <20230624092330.157338-6-benno.lossin@proton.me> In-Reply-To: <20230624092330.157338-1-benno.lossin@proton.me> References: <20230624092330.157338-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no 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 Add two functions `pin_init_array_from_fn` and `init_array_from_fn` that take a function that generates initializers for `T` from usize, the added functions then return an initializer for `[T; N]` where every element is initialized by an element returned from the generator function. Suggested-by: Asahi Lina Signed-off-by: Benno Lossin --- rust/kernel/init.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 44bc3e77419a..c9ea4bf71987 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -867,6 +867,96 @@ pub fn uninit() -> impl Init, E> = { unsafe { init_from_closure(|_| Ok(())) } } +/// Initializes an array by initializing each element via the provided ini= tializer. +/// +/// # Examples +/// +/// ```rust +/// let array: Box<[usize; 1000_000_000]>=3D Box::init(init_array_from_fn(= |i| i)).unwrap(); +/// println!("{array:?}"); +/// ``` +pub fn init_array_from_fn( + mut make_init: impl FnMut(usize) -> I, +) -> impl Init<[T; N], E> +where + I: Init, +{ + let init =3D move |slot: *mut [T; N]| { + let slot =3D slot.cast::(); + for i in 0..N { + let init =3D make_init(i); + // SAFETY: since 0 <=3D `i` < N, it is still in bounds of `[T;= N]`. + let ptr =3D unsafe { slot.add(i) }; + // SAFETY: The pointer is derived from `slot` and thus satisfi= es the `__init` + // requirements. + match unsafe { init.__init(ptr) } { + Ok(()) =3D> {} + Err(e) =3D> { + // We now free every element that has been initialized= before: + for j in 0..i { + let ptr =3D unsafe { slot.add(j) }; + // SAFETY: The value was initialized in a previous= iteration of the loop + // and since we return `Err` below, the caller wil= l consider the memory at + // `slot` as uninitialized. + unsafe { ptr::drop_in_place(ptr) }; + } + return Err(e); + } + } + } + Ok(()) + }; + // SAFETY: The initializer above initializes every element of the arra= y. On failure it drops + // any initialized elements and returns `Err`. + unsafe { init_from_closure(init) } +} + +/// Initializes an array by initializing each element via the provided ini= tializer. +/// +/// # Examples +/// +/// ```rust +/// let array: Arc<[Mutex; 1000_000_000]>=3D +/// Arc::pin_init(init_array_from_fn(|i| new_mutex!(i))).unwrap(); +/// println!("{array:?}"); +/// ``` +pub fn pin_init_array_from_fn( + mut make_init: impl FnMut(usize) -> I, +) -> impl PinInit<[T; N], E> +where + I: PinInit, +{ + let init =3D move |slot: *mut [T; N]| { + let slot =3D slot.cast::(); + for i in 0..N { + let init =3D make_init(i); + // SAFETY: since 0 <=3D `i` < N, it is still in bounds of `[T;= N]`. + let ptr =3D unsafe { slot.add(i) }; + // SAFETY: The pointer is derived from `slot` and thus satisfi= es the `__pinned_init` + // requirements. + match unsafe { init.__pinned_init(ptr) } { + Ok(()) =3D> {} + Err(e) =3D> { + // We now have to free every element that has been ini= tialized before, since we + // have to abide by the drop guarantee. + for j in 0..i { + let ptr =3D unsafe { slot.add(j) }; + // SAFETY: The value was initialized in a previous= iteration of the loop + // and since we return `Err` below, the caller wil= l consider the memory at + // `slot` as uninitialized. + unsafe { ptr::drop_in_place(ptr) }; + } + return Err(e); + } + } + } + Ok(()) + }; + // SAFETY: The initializer above initializes every element of the arra= y. On failure it drops + // any initialized elements and returns `Err`. + unsafe { pin_init_from_closure(init) } +} + // SAFETY: Every type can be initialized by-value. unsafe impl Init for T { unsafe fn __init(self, slot: *mut T) -> Result<(), E> { -- 2.41.0