From patchwork Fri Feb 24 09:11:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 61199 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp1211070dyk; Fri, 24 Feb 2023 01:13:42 -0800 (PST) X-Google-Smtp-Source: AK7set/nagu/bLKNbKxGDgX6+DwQ03sTeh21keGKH2WuFOPagH/4gg+0LpZ6OvEH0dakSRBZcfD+ X-Received: by 2002:a17:903:846:b0:198:a372:3e67 with SMTP id ks6-20020a170903084600b00198a3723e67mr12857583plb.27.1677230021860; Fri, 24 Feb 2023 01:13:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677230021; cv=none; d=google.com; s=arc-20160816; b=TrSOP4T4GFSWSitpVGkZW5Wpf02X5dPIhtoYLibhbQEVCY5/ZLq4TFWWYgEN7qp1uv tJYXGyY3zgn23F5W6BcwXrn9VfvdDYRQhlev8BG4HQNy2Y5E+TvAiWQST5ULH1cyKm2T vdSB9E5VArwVdkncai9xI0nn/wpM131h7zrWhwhWzVCyUwYbKNF12FhA7PKbH0vJIZ0c enABEqA+v/dCa9KnRkvkshWF95NElABNST/e/9jCs3EiYw8Lu9keaH6KtMK2dR9J+QEL q4swXgndKavuNEPFYkDJdIy5KL5PAAzmFzBiINPoUDnWWNoIDD/+MjO9s6MtaWENdVhM LoEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=TCBOmHiE92jGGI2W9B+9msKzDn5dlR6WFk3nrkIOAiI=; b=I+jyGWVa6ttV5rD3/kEhhkaoVyZV7Be0BW0YpvYP6C4jU9m3V1uk0kL8ApGNA3hXTK fxQ1krBUFCL3A5tUbjlAfAa3VTZtzF0PCNsFTnELIAy02YYRXw9V+cV6y7m9eM+pEvGc thtdPxiitYjMhxHKsvuDu24WVWbINKfTfd0gPb4YM7Q2Phftg4XhCdKRnr0rHhUGmGZa HJs8sy1/BNNfZfsCzHJ/gI26u6gOYLu+M5MwEW/BMEwoVRXbIqV4EiymGJ+oCPXn8oKc y5pOi7j0tbB3JZwmAW4hWZIVtU34nDTjewxDa3DBQDIXxvwaRrl1GxZ82xmToQQ/N8KW ofoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=aq+DnANa; 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=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l12-20020a170902f68c00b0019aba699d56si18446633plg.608.2023.02.24.01.13.29; Fri, 24 Feb 2023 01:13:41 -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=@asahilina.net header.s=default header.b=aq+DnANa; 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=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229770AbjBXJMN (ORCPT + 99 others); Fri, 24 Feb 2023 04:12:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229689AbjBXJMJ (ORCPT ); Fri, 24 Feb 2023 04:12:09 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E544765CDB; Fri, 24 Feb 2023 01:12:07 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id E58264247C; Fri, 24 Feb 2023 09:12:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677229926; bh=aEnf3Yi6W7s32UBEQhPMTbXySskKZPdQMXcNDxihEz0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=aq+DnANaXmRdkfeWfOIHDk/XU/UrjeHhFYVdtIKu5egVbFN12LN9vWxZBvAXAav6k sOz0sOzcVA3lM4+8CzVqCT+IyWL8nxsJznfkH5EigOR9HSE9v+SpfRWg2pw2zThu3f PGSnyW/YQA/l3oGEEFnunZOtMXdvRiGozGcTk8NcB0hn01RSlz2KbaHkML/FAADRis 6jgjkJIV4u4kmZTuy2KI5MrtAEA3tBMSxQEMoDSGLU/yl8F2DJ7oifZJauUCnAzcQJ 5R+bHCtq0eW9Q3CD4HfBIu/geA5VX5BskkTuK+LAIQXYPQkZzqB2t43lfMDoRt/jep pAmiXv89FdAvw== From: Asahi Lina Date: Fri, 24 Feb 2023 18:11:48 +0900 Subject: [PATCH 2/4] rust: Import upstream `alloc::vec::spec_extend` module MIME-Version: 1.0 Message-Id: <20230224-rust-vec-v1-2-733b5b5a57c5@asahilina.net> References: <20230224-rust-vec-v1-0-733b5b5a57c5@asahilina.net> In-Reply-To: <20230224-rust-vec-v1-0-733b5b5a57c5@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1677229917; l=3800; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=aEnf3Yi6W7s32UBEQhPMTbXySskKZPdQMXcNDxihEz0=; b=g5G05vV5FAJKNC2CQVVFo0gberglSfKhbAQEz33O9+FHYNCX8NT77Z0NrrJRm4nipKklHn4o6 YHpmy1B6hvuCVQnVWkckoagSMkhGg9xHkrDIcEtJt+KIyZ1FYPaTxi+ X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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?1758703147206896207?= X-GMAIL-MSGID: =?utf-8?q?1758703147206896207?= This is a subset of the Rust standard library `alloc` crate, version 1.66.0, licensed under "Apache-2.0 OR MIT", from: https://github.com/rust-lang/rust/tree/1.66.0/library/alloc/src The file is copied as-is, with no modifications whatsoever (not even adding the SPDX identifiers). For copyright details, please see: https://github.com/rust-lang/rust/blob/1.66.0/COPYRIGHT Signed-off-by: Asahi Lina --- rust/alloc/vec/spec_extend.rs | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/rust/alloc/vec/spec_extend.rs b/rust/alloc/vec/spec_extend.rs new file mode 100644 index 000000000000..1ea9c827afd7 --- /dev/null +++ b/rust/alloc/vec/spec_extend.rs @@ -0,0 +1,87 @@ +use crate::alloc::Allocator; +use core::iter::TrustedLen; +use core::ptr::{self}; +use core::slice::{self}; + +use super::{IntoIter, SetLenOnDrop, Vec}; + +// Specialization trait used for Vec::extend +pub(super) trait SpecExtend { + fn spec_extend(&mut self, iter: I); +} + +impl SpecExtend for Vec +where + I: Iterator, +{ + default fn spec_extend(&mut self, iter: I) { + self.extend_desugared(iter) + } +} + +impl SpecExtend for Vec +where + I: TrustedLen, +{ + default fn spec_extend(&mut self, iterator: I) { + // This is the case for a TrustedLen iterator. + let (low, high) = iterator.size_hint(); + if let Some(additional) = high { + debug_assert_eq!( + low, + additional, + "TrustedLen iterator's size hint is not exact: {:?}", + (low, high) + ); + self.reserve(additional); + unsafe { + let mut ptr = self.as_mut_ptr().add(self.len()); + let mut local_len = SetLenOnDrop::new(&mut self.len); + iterator.for_each(move |element| { + ptr::write(ptr, element); + ptr = ptr.add(1); + // Since the loop executes user code which can panic we have to bump the pointer + // after each step. + // NB can't overflow since we would have had to alloc the address space + local_len.increment_len(1); + }); + } + } else { + // Per TrustedLen contract a `None` upper bound means that the iterator length + // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway. + // Since the other branch already panics eagerly (via `reserve()`) we do the same here. + // This avoids additional codegen for a fallback code path which would eventually + // panic anyway. + panic!("capacity overflow"); + } + } +} + +impl SpecExtend> for Vec { + fn spec_extend(&mut self, mut iterator: IntoIter) { + unsafe { + self.append_elements(iterator.as_slice() as _); + } + iterator.forget_remaining_elements(); + } +} + +impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec +where + I: Iterator, + T: Clone, +{ + default fn spec_extend(&mut self, iterator: I) { + self.spec_extend(iterator.cloned()) + } +} + +impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec +where + T: Copy, +{ + fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { + let slice = iterator.as_slice(); + unsafe { self.append_elements(slice) }; + } +}