Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp3356887rwo; Mon, 24 Jul 2023 09:47:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlFtMxzqyDaxqSgVBxK19Jgz1CqeD2lZOQq8LVHl7LCUgexpV3E8k5wt4IK5ZWlhnqHVCtXl X-Received: by 2002:a17:90b:11c9:b0:268:f45:c10a with SMTP id gv9-20020a17090b11c900b002680f45c10amr2462758pjb.26.1690217247200; Mon, 24 Jul 2023 09:47:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690217247; cv=none; d=google.com; s=arc-20160816; b=YxXIDm1xgkOyJGe+RfXtfZRLSlvDI3EZ3vw4k5V2o5anVqVFMGxwoszeuiFaEBNqgA rFPOAp7Fpc4tJv1ZIhLCCCO3Xm7a4n2pFeDE1bInZ0OE25ZPztEjmb89cqllRF/NeTen A6zUlNx2925wZn/YJLKOgVI7FLDEyiJG7/cGJ8CBRdUjfxSO26tPJHVB6m3HQTHIGWNa PftWIZlz+Sy+Z23uZFGs2rex06Ol6FyRIqTaRUM+yP09kJ3HoLzyr0AIk7uH4+ybSvSS la2ah2hBHFWyNmTdMnYEBgo6qAqzzr1/1Y1jS//M8ry86071KRToifWK7fx7SVZt+XFK xNvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=sNi5vVMCJWfHjXXkqvqwnxz9gP61j7y0zdFiLD2AOm4=; fh=QIytb8Pc/tNQKCXmppbqLtpd0hrzmewt+SvaGVZrVJI=; b=bmOO8srAFwT7f24AG8P7tOD3scIIZcmPbL3aKquBCSUdtj0FIPoD/iChYqFCAYxIrP d0KKyU1dmM+hWvQ5Disjm6Rqphp+aMzsFCw58QBrM8ceOFZAFqX3YaQ+MMoNApSG0lQP eCl/aQoa35Gax0IiWpfbKxIo9YO3wGckELtBVcKA/oYU+K/OtDKqb8t7JH557Fjr2YpR 4luqxZzcG8IGYMf1q4PkIpmtWSQt/nZHlfvPEIlLxKDZBfIgd2DtBUYoauTYvNIr3m5H /MRFIeSwJY26OzywxBDXpo4xA8DRCLCeDMJzU0fUYxmRomoHBkSV4JQ2ps+WIUbUVnZ2 VX4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=b1cbmfyK; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s1-20020a63dc01000000b00563522a7b7asi9918108pgg.14.2023.07.24.09.47.14; Mon, 24 Jul 2023 09:47:27 -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=@gmail.com header.s=20221208 header.b=b1cbmfyK; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229897AbjGXQHP (ORCPT + 99 others); Mon, 24 Jul 2023 12:07:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230186AbjGXQHL (ORCPT ); Mon, 24 Jul 2023 12:07:11 -0400 Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D73CD10FF; Mon, 24 Jul 2023 09:07:09 -0700 (PDT) Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-6b9aadde448so4066664a34.0; Mon, 24 Jul 2023 09:07:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690214829; x=1690819629; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=sNi5vVMCJWfHjXXkqvqwnxz9gP61j7y0zdFiLD2AOm4=; b=b1cbmfyKHmtLdjno0V/bvAf32VvNFJpzM2VD9rlw08X9zpd1njJBjfqc/4AQ++uSbl Dm2JllG46Lyxx3hPZ9KJMj6B0UPhnnC9OROEcysatpXE5tJ2x9QTbp+z9nmC7mFdGArS SJoRedyiX6G/HPiW5OJcoE0H0fpz7CIZdKJ08//8arjk2akZHVkrHM+AO9qzmYp5x9bI 4/U+gAoQwOoq4u3ki7jpL0BHB6uu69D8nGrch3zYWUQA0eAmx2Ze4G6iydhata5WgXrR wP+jMiuUv9E4Ch6BJVUaYYyvoskSeTxRoSmXPvuRCnLNKCchY2UU+RAiWA8Pkoey9Z8k Z/cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690214829; x=1690819629; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sNi5vVMCJWfHjXXkqvqwnxz9gP61j7y0zdFiLD2AOm4=; b=fmWoXkQhxKWuWds8plPew+uUHCaTgEzFOojR6ypd9su91NwhToioPhgP2w+kBQvayb ER699vBOorEOxyLxTTzWfE0hkDes7vKKbyeDf1daYKVU8cVqubvtgiU1b6n55HPp7lsg 8j/0OJwL6q03BBBBiagoPDx51jIqeK48cMeMXv6XiocqE/Oryy5Jf91CvVFp37lQ95KE J1Sj0NNILoKrQnGDFI1KgMvXQP3rp6XWaY6PT8KDA1MysC/Q+Yo4+3pqvLiMWAZevYwd pn+hvGE6o+XpSX9EpreU9e4S8yttRG3ykxW7XMW4PsAFE/JD7WEiN4lPGHSQcgTDbVMF TAew== X-Gm-Message-State: ABy/qLZUGN4qteK9s7FgjeEpGyVWF2D9bT/XQffykXQMvXTpnK1HczfN RoIqCls6ArMvGH1kAPbkyAQ= X-Received: by 2002:a05:6830:11da:b0:6b9:99fe:4747 with SMTP id v26-20020a05683011da00b006b999fe4747mr9267660otq.29.1690214829009; Mon, 24 Jul 2023 09:07:09 -0700 (PDT) Received: from [192.168.54.90] (static.220.238.itcsa.net. [190.15.220.238]) by smtp.gmail.com with ESMTPSA id p3-20020a9d6943000000b006b9734b9fafsm4024904oto.13.2023.07.24.09.07.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Jul 2023 09:07:08 -0700 (PDT) Message-ID: Date: Mon, 24 Jul 2023 13:07:04 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 11/12] rust: init: add `{pin_}chain` functions to `{Pin}Init` Content-Language: en-US To: Benno Lossin , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Asahi Lina References: <20230719141918.543938-1-benno.lossin@proton.me> <20230719141918.543938-12-benno.lossin@proton.me> <0b818707-4762-c12d-8624-7d3c4f6841da@gmail.com> <5f22b25d-132d-7cbc-8bca-8333516c1663@proton.me> From: Martin Rodriguez Reboredo In-Reply-To: <5f22b25d-132d-7cbc-8bca-8333516c1663@proton.me> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On 7/24/23 11:08, Benno Lossin wrote: > On 21.07.23 02:23, Martin Rodriguez Reboredo wrote: >> On 7/19/23 11:21, Benno Lossin wrote: >>> +/// An initializer returned by [`PinInit::pin_chain`]. >>> +pub struct ChainPinInit(I, F, __internal::Invariant<(E, Box)>); >>> + >>> +// SAFETY: the `__pinned_init` function is implemented such that it >>> +// - returns `Ok(())` on successful initialization, >>> +// - returns `Err(err)` on error and in this case `slot` will be dropped. >>> +// - considers `slot` pinned. >>> +unsafe impl PinInit for ChainPinInit >>> +where >>> + I: PinInit, >>> + F: FnOnce(Pin<&mut T>) -> Result<(), E>, >>> +{ >>> + unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { >>> + // SAFETY: all requirements fulfilled since this function is `__pinned_init`. >>> + unsafe { self.0.__pinned_init(slot)? }; >>> + // SAFETY: The above call initialized `slot` and we still have unique access. >>> + let val = unsafe { &mut *slot }; >>> + // SAFETY: `slot` is considered pinned >>> + let val = unsafe { Pin::new_unchecked(val) }; >>> + (self.1)(val).map_err(|e| { >>> + // SAFETY: `slot` was initialized above. >>> + unsafe { core::ptr::drop_in_place(slot) }; >>> + e >> >> I might stumble upon an error like EAGAIN if I call `pin_chain` but that >> means `slot` will be dropped. So my recommendation is to either not drop >> the value or detail in `pin_chain`'s doc comment that the closure will >> drop on error. > > This is a bit confusing to me, because dropping the value on returning `Err` > is a safety requirement of `PinInit`. Could you elaborate why this is > surprising? I can of course add it to the documentation, but I do not see > how it could be implemented differently. Since if you do not drop the value > here, nobody would know that it is still initialized. I knew about the requirement of dropping on `Err`, but what has caught my attention is that `{pin_}chain` might not abide with it per the doc comment as it says that `self` is initialized before calling `f`... /// First initializes the value using `self` then calls the function /// `f` with the initialized value. But one can not know what would happen when `f` fails, specially if such failure can be ignored or it's only temporarily. So then, the best course IMO is to mention that in some way the value is still being initialized, kinda setting it up, and that it will be dropped when an error is returned. WDYT?