From patchwork Mon Jan 30 06:44:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 50150 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2038657wrn; Sun, 29 Jan 2023 22:46:52 -0800 (PST) X-Google-Smtp-Source: AMrXdXuhzGTa8NvJmX+OzGcHv/ZOGd38vQjYGPqeodaM1xn0dkw9sqeC5bmOdh8RtGsk3oJfeRKQ X-Received: by 2002:a05:6402:4447:b0:497:c96b:4dea with SMTP id o7-20020a056402444700b00497c96b4deamr108345304edb.5.1675061132246; Sun, 29 Jan 2023 22:45:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675061132; cv=none; d=google.com; s=arc-20160816; b=awOSDT+JcsthVSkdCK1gDFD+bMT6gBNiXlUGiWSRUkt0dPF8JWJUL2aVFUOT9droP9 dbHCOhfKjqz42Yz1s21tnXB2WSjBhWNhfcTi3WxuS7mQVmshnsworxZ190hKLoKeZKcK /qoLUctmAVz5f7jYyHz7uBtxRLxZqKCoU/wxFhRyf/F7xHQngO9kwaBVSngJsG336L+0 3unbjzVfHAB2c2zRGhNq6SP2lg/lduwa/DKFp1UPhUtHRrNObKBKg9F8L6ncX+TDu/bk FQwwJdAHnDwIo/SkqZ/hp2XksSXLXaNmWgOlg60EunQV6TNSvHGZ3IqooC1knK2zUZbz d+Dg== 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=lcoHsPxq8wVGl54MVPcRLlL61B4LcEe/VoM4sx/i6CI=; b=g8eeyCIylojBa6RaUnBX8O2FAIAHzE8ZI7lw/LgbnT4MRRkuSqON68kHxRRrrDf7XY rnlZ6Z9NmZwCgdlGpSqymH5aOMphD9j7BAMc/rHntptBU0dPArErgG52wyQ8gf6ChlCS nUilHKuEg3ZmjCYGufhiSMKBRiCynFx8LIGyGTEyhFPE39+m6mFGDxId4xypo7i3TIGb bNlrkyznNDrptEf+xg4z1NYpkBML7S7G0vgz1Q9O3aqrYcdrBmgc8KlqyTdf9xKoU8tt YoHiwMV7UR9yJGBTRRL+kL6e+CYbTWEgbRmAmcA+otaNDOwwSUaMBtCeELYUp/PKwAke Ta3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GdCNzoVy; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j13-20020aa7ca4d000000b004a1e52a5415si10068836edt.367.2023.01.29.22.45.08; Sun, 29 Jan 2023 22:45:32 -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=@gmail.com header.s=20210112 header.b=GdCNzoVy; 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 S235798AbjA3Gok (ORCPT + 99 others); Mon, 30 Jan 2023 01:44:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235746AbjA3Goc (ORCPT ); Mon, 30 Jan 2023 01:44:32 -0500 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54A911423B; Sun, 29 Jan 2023 22:44:31 -0800 (PST) Received: by mail-oi1-x22d.google.com with SMTP id dt8so6166399oib.0; Sun, 29 Jan 2023 22:44:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=lcoHsPxq8wVGl54MVPcRLlL61B4LcEe/VoM4sx/i6CI=; b=GdCNzoVy6C9eZXiBn/+g07Yxh+x4Eeck7GAtnKymZzppORxSaPwYrdriEz8uP+7gxD 3clET0B2eZZaHsyIN+hca4/6PTIzBgIUGnf1RtHoCXODNzq/GQvPKDpkk8mmzTJc4cHc SBEm0hFrWt6ObonlYgUYGA9b9uP29FeaFHBfDmlCulH/BMrl+F+UnPJOgETRoCbWQ1p7 7YrKFdz9V6eCIMEAmnHZx4jRsNQnJjFO3D1ctvHXWACo0h4t27YNcl9Oqk/x9KFJ9YL4 BvzyNhoQtSNWS9+Q3bi1iwGy4Hq/zEibw3lEzIeETY3ZU5IU8gC/cfUIGhhesZ7A6eU6 KjkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=lcoHsPxq8wVGl54MVPcRLlL61B4LcEe/VoM4sx/i6CI=; b=isNs9o4BmYpiPfF696iYyNH5H9AeadEo80egiewMau0Py62eZC343okBHMhwguMkJh ywEJnBHed9eyqp6WRvjtVvlmkdM+JRB58oeAexbygXDS+KE+v2cYaWw8QwJ3B+LZjQ8+ K/Ev9Z7yr4K62pA0Qk3fdo3RrO9+yeaiyBUzYCH9evbmo30YopzjxD1dSsnC3TnH1r2X fF6b9iucsoSTbw0UWOvuysUEMzhNiNncfWgnh58OTUFyMDG+IfePU5LR62yOJ6sWeira dhtf0jG248p+WYmVJJyS39ECgXilk5volVWAEv+nV1wL7K0Bt/WcEyf/WVGmUCCqD8Eo 3wnw== X-Gm-Message-State: AFqh2koI73/p36BiKmKTcoo/a0or8PC3k2ge53x+Lsz20c825qC/8Aft MIoI2Toa9yR3d1ZSxEYbFRTlypALyLYCbaCm X-Received: by 2002:a05:6808:3186:b0:36e:bab6:cb71 with SMTP id cd6-20020a056808318600b0036ebab6cb71mr20074711oib.50.1675061070677; Sun, 29 Jan 2023 22:44:30 -0800 (PST) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id o64-20020aca5a43000000b003670342726fsm4427883oib.12.2023.01.29.22.44.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 22:44:30 -0800 (PST) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Vincenzo Palazzo , Martin Rodriguez Reboredo Subject: [PATCH v2 2/5] rust: types: introduce `ForeignOwnable` Date: Mon, 30 Jan 2023 03:44:01 -0300 Message-Id: <20230130064404.744600-2-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230130064404.744600-1-wedsonaf@gmail.com> References: <20230130064404.744600-1-wedsonaf@gmail.com> MIME-Version: 1.0 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 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?1756428901847893593?= X-GMAIL-MSGID: =?utf-8?q?1756428901847893593?= It was originally called `PointerWrapper`. It is used to convert a Rust object to a pointer representation (void *) that can be stored on the C side, used, and eventually returned to Rust. Signed-off-by: Wedson Almeida Filho Reviewed-by: Vincenzo Palazzo Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Gary Guo Reviewed-by: Andreas Hindborg --- v1 -> v2: Use `Self` instead of generic type in `borrow_mut` rust/kernel/lib.rs | 1 + rust/kernel/types.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index e0b0e953907d..223564f9f0cc 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -16,6 +16,7 @@ #![feature(coerce_unsized)] #![feature(core_ffi_c)] #![feature(dispatch_from_dyn)] +#![feature(generic_associated_types)] #![feature(receiver_trait)] #![feature(unsize)] diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index dd834bfcb57b..72710b7442a3 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -8,6 +8,60 @@ use core::{ ops::{Deref, DerefMut}, }; +/// Used to transfer ownership to and from foreign (non-Rust) languages. +/// +/// Ownership is transferred from Rust to a foreign language by calling [`Self::into_foreign`] and +/// later may be transferred back to Rust by calling [`Self::from_foreign`]. +/// +/// This trait is meant to be used in cases when Rust objects are stored in C objects and +/// eventually "freed" back to Rust. +pub trait ForeignOwnable: Sized { + /// Type of values borrowed between calls to [`ForeignOwnable::into_foreign`] and + /// [`ForeignOwnable::from_foreign`]. + type Borrowed<'a>; + + /// Converts a Rust-owned object to a foreign-owned one. + /// + /// The foreign representation is a pointer to void. + fn into_foreign(self) -> *const core::ffi::c_void; + + /// Borrows a foreign-owned object. + /// + /// # Safety + /// + /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for + /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet. + /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow_mut`] + /// for this object must have been dropped. + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>; + + /// Mutably borrows a foreign-owned object. + /// + /// # Safety + /// + /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for + /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet. + /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and + /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped. + unsafe fn borrow_mut(ptr: *const core::ffi::c_void) -> ScopeGuard { + // SAFETY: The safety requirements ensure that `ptr` came from a previous call to + // `into_foreign`. + ScopeGuard::new_with_data(unsafe { Self::from_foreign(ptr) }, |d| { + d.into_foreign(); + }) + } + + /// Converts a foreign-owned object back to a Rust-owned one. + /// + /// # Safety + /// + /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for + /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet. + /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and + /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped. + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self; +} + /// Runs a cleanup function/closure when dropped. /// /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running. From patchwork Mon Jan 30 06:44:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 50153 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2039258wrn; Sun, 29 Jan 2023 22:49:33 -0800 (PST) X-Google-Smtp-Source: AK7set+TXjRgxmZ8+FjoPB25qnq11ONViS0AByWKtors4motzc7haFslO0bkrLoVuLWbpH2UjomG X-Received: by 2002:a05:6a21:3295:b0:bc:c663:41be with SMTP id yt21-20020a056a21329500b000bcc66341bemr6582724pzb.30.1675061373586; Sun, 29 Jan 2023 22:49:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675061373; cv=none; d=google.com; s=arc-20160816; b=TNRYsy1v10M1FnRTJXd6s9O4TmqRE71RpC6Z0XbX1DqzO3G5ea9c4mRaiVFYas7QaG DLHemi0fkPz4VfUl2/09AgoGULubUQzYKEU5gkPDNy/QYP9aGKP+FyDy01iOaNN7BIEf W5wBg74SF7+44pg9ZzQS92KEJs+Hzz6fMMYJ42tYJAOev+8O/wLWxoUZt0kMjcDo9VdQ tK5oFIrSbuw4qsd/hnODnI0U46V8c8cbR/hIznBcQWvMktIe1PXNLlpexKy3zb8xy7wB HjtyIqMkE+AL4cqe5uw6nJOExeoYhiP202CZyBWToSf1SwKcFxM4bvHrnDWvbuSZCMpf 5Wtw== 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=eWJyUEkZEQaGfIphT2kEHbYhQK8gjFMeU5IeqaqkaN8=; b=L0F9G4kIbMxFFqEBa3D1aU/DwWLnsbUQYKeyeX0jvAZ3UfFlBuUm4lx5jUQ7k7c4/o cQoZbSxB73MTCbJU/JpXWo3FPFofAPkPelzXAzNguNSSzFQR0AdbpPx6qNf8aV6kY5Nx J8M6QDDq7SCZ8Y1qid2ImxNdwz88ZkDRs9PW2lY4KFqeaXVkj+cQc+pk50qjE+Nx4WEx zNyNEFOsQWzaw7vCQ/UdnefWCPSHgSmd+crDoD4jpnk1k6s14FHkxsvmAhefadPJEW2E valFWiuTt0BRYIhw91jMcPlsNw90LViOF+TuHXGtGPrknaTtf+4aKBAWDnqSQMMQR05z 1H1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=I5vCrxtE; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 129-20020a630087000000b004e541ab795fsi5654312pga.270.2023.01.29.22.49.21; Sun, 29 Jan 2023 22:49:33 -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=@gmail.com header.s=20210112 header.b=I5vCrxtE; 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 S235836AbjA3Gop (ORCPT + 99 others); Mon, 30 Jan 2023 01:44:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235820AbjA3Gok (ORCPT ); Mon, 30 Jan 2023 01:44:40 -0500 Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E11E71556F; Sun, 29 Jan 2023 22:44:36 -0800 (PST) Received: by mail-oi1-x229.google.com with SMTP id p133so9243767oig.8; Sun, 29 Jan 2023 22:44:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=eWJyUEkZEQaGfIphT2kEHbYhQK8gjFMeU5IeqaqkaN8=; b=I5vCrxtE7OddIrmBOvuhwg/Vyh7yMyA0GCYe32UT8twm4O6URjG0kOMGqPjcQfRbl6 lyIjY8Hyzvcl+SigwcHOqgQ1ZW25KJg/J2VgJ4tRcU+oO/m7ce7GokbIw+64d4QaAItf AtM35hI8QevoZ4PmD9dFyb6s/VfgFKborWlJKaT4ihLADxiDmsjlA/rhVNkn6H3m/5HX gfTeB3LlOof60rNB0mQ/b0hfKxVMLXlomHU9WWCbI8XwDkNf8Sz0cSLNIaKw02KfIy1v tLBtYSlpwKeoTIQqo6dx/S754EMc/6oxHcbcWk5WEP2TFmbg734XUPyScvG4l/iXYOil vRAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=eWJyUEkZEQaGfIphT2kEHbYhQK8gjFMeU5IeqaqkaN8=; b=v6vwTMr46srDj0vZH/QBBbPwi5kEO0C+NslSTCbOxsxJuX/+0pr6QwvzOgVBDhGVpC lWzrHHu+nKi0p7Elov2zLTDP3Rjej9Eg4ETDdR+K/idRoB5XC5T+PEA4npRPsiA2FHJc 74bgpfh8RsGhsq/oira7wEV/2yL+l43cspMx5LCZVouJuyNZjWyL4MgB8KCjr/VMkdUr L1W8/vOwUh329kPvmXRuQ5mDZ2iUUb+9E3h/4rxK5p09/mqpscenK0l643tN1weYa9l8 OxpxtYmefgHwveT7OY5HrlKuL752g8SkD9bWe8WO1f9sNVlwEPBQZ3YkSj5jDHMIEruX Sr/w== X-Gm-Message-State: AO0yUKWFMwOHoyqadkfa/rPc8p6+akBBBu7uk0Is9g7XRQoU+3Jdq+oK xCumgxr8Ndsw2vsLzQ438VbJNisL8ukngi2U X-Received: by 2002:a05:6808:b25:b0:378:1ae6:86b7 with SMTP id t5-20020a0568080b2500b003781ae686b7mr2661128oij.35.1675061076234; Sun, 29 Jan 2023 22:44:36 -0800 (PST) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id o64-20020aca5a43000000b003670342726fsm4427883oib.12.2023.01.29.22.44.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 22:44:36 -0800 (PST) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Vincenzo Palazzo , Alice Ferrazzi Subject: [PATCH v2 3/5] rust: types: implement `ForeignOwnable` for `Box` Date: Mon, 30 Jan 2023 03:44:02 -0300 Message-Id: <20230130064404.744600-3-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230130064404.744600-1-wedsonaf@gmail.com> References: <20230130064404.744600-1-wedsonaf@gmail.com> MIME-Version: 1.0 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 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?1756429155043682333?= X-GMAIL-MSGID: =?utf-8?q?1756429155043682333?= This allows us to hand ownership of Rust dynamically allocated objects to the C side of the kernel. Signed-off-by: Wedson Almeida Filho Reviewed-by: Gary Guo Reviewed-by: Vincenzo Palazzo Reviewed-by: Alice Ferrazzi Reviewed-by: Andreas Hindborg --- v1 -> v2: Add `use alloc::boxed::Box`, which wasn't needed before rust/kernel/types.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 72710b7442a3..411655eca3e9 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -2,6 +2,7 @@ //! Kernel types. +use alloc::boxed::Box; use core::{ cell::UnsafeCell, mem::MaybeUninit, @@ -62,6 +63,28 @@ pub trait ForeignOwnable: Sized { unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self; } +impl ForeignOwnable for Box { + type Borrowed<'a> = &'a T; + + fn into_foreign(self) -> *const core::ffi::c_void { + Box::into_raw(self) as _ + } + + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T { + // SAFETY: The safety requirements for this function ensure that the object is still alive, + // so it is safe to dereference the raw pointer. + // The safety requirements of `from_foreign` also ensure that the object remains alive for + // the lifetime of the returned value. + unsafe { &*ptr.cast() } + } + + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous + // call to `Self::into_foreign`. + unsafe { Box::from_raw(ptr as _) } + } +} + /// Runs a cleanup function/closure when dropped. /// /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running. From patchwork Mon Jan 30 06:44:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 50151 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2038781wrn; Sun, 29 Jan 2023 22:47:30 -0800 (PST) X-Google-Smtp-Source: AK7set+dQ5bZiwQz+33t55s8AfBW9MTTgarc9aX3e3BYKL9SJCoX95HoHPtnRxORMp3mSWgPxV2N X-Received: by 2002:a17:907:2bc2:b0:885:fcbd:40d3 with SMTP id gv2-20020a1709072bc200b00885fcbd40d3mr5328222ejc.8.1675061250484; Sun, 29 Jan 2023 22:47:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675061250; cv=none; d=google.com; s=arc-20160816; b=KEHaqXSWEfPEPb+bz2gfErLN8Gqn6Jlgh5T5u3A8Z92PKKK/zeyspO6Rr4dbdhm8ou FEmvol3dfmy9s4bsttltSCmLgs40wB+f3WWwjaca9348qKz5MQLlblMQR+zGwd9+djc6 8lRZR/eXrN94GXfncFfIHXKYADUeV890y1bme7Uyf7vc+nLgXlvySh6AbzqBxMynX/EV CvH5Xf9NZJjHyiM3rmnCCCi17dySHfj8TdSjIDAiqwJuOhCGWESEj+sFmyngfmDdlaeF dc+IwOkf9LZ9T+zwNs05n695nyH/L28VD+zIMBL86o30yHLxTdQzh2jd78ElyRsya323 cLQg== 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=1+8DLLeJL2vbcZ/1sx6aN3cI3SB5furv18W3lUJcflM=; b=0CFBrlW8wu0uHXU4QLw+/RX9t2BL8Z8EodCCMPk0nySWa0edOIepyYaUXAuq6cvQ9u PPD/cu6Od0VFah77XUqdJpoVHvelIYQvfU+yXp551K9vU9/c4CjEQJJBoh4/isPmDBCl f3N7Kt5h1NyqAnzQdyhqe+a1yrRRGUjmmMdNVvLdhdrhqw+VYueBbJ7B/F8YQ1H4+jFl eproPjqE9D/4/ItLzWeaAag2rU5dl3AJ7lsnC+Jg877Phsiq6L8b0XvVCPr9Ns/+4SlZ FPIC5vd752xqmbKVD0H3+u3NwCWEH3FBT4No8Grss/yy9XcJTM6ZPLVmX4WMAwbcEEdH sfYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TfF3Ypmn; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 19-20020a17090602d300b007c0b56b90f5si12431582ejk.307.2023.01.29.22.47.07; Sun, 29 Jan 2023 22:47:30 -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=@gmail.com header.s=20210112 header.b=TfF3Ypmn; 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 S235859AbjA3GpI (ORCPT + 99 others); Mon, 30 Jan 2023 01:45:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235835AbjA3Gop (ORCPT ); Mon, 30 Jan 2023 01:44:45 -0500 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D737013DF8; Sun, 29 Jan 2023 22:44:40 -0800 (PST) Received: by mail-oi1-x22d.google.com with SMTP id dt8so6166611oib.0; Sun, 29 Jan 2023 22:44:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=1+8DLLeJL2vbcZ/1sx6aN3cI3SB5furv18W3lUJcflM=; b=TfF3Ypmncs4V09HuumYfpFm2C0xZDaLKRAETSnCb/bJZmvUNrMXwDR1J/l2c+VxFAJ VBfW1vtGgiOBjxi4NWhvcIR2B52khFq6DOz4uCVsmIcUXJ7izYbDofHb5RnENJhF4ogR gbq94kf5XPsGkRU5sDtpAyCRrtfy8/YVeNt2nWXhlU4VbBJNiwVTDNYoP/4G+DNpIpVF W3MIZA7jAKnuiMoxDcbA7uG/Lm7wFs1nBXQssgXV+OcpwhasNu5LuEhF+GKI9TI4TnaL 08Uv8P2uQdUAqTDOPv9MFFKhegpm6H0ByTzb0Ng4PySW/1r8wclHhhpNtjd5oMBhxOkU qsFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=1+8DLLeJL2vbcZ/1sx6aN3cI3SB5furv18W3lUJcflM=; b=SGfIV6y+/L8vmFoH2NGTdzpyL0LpeczeUFcTMIX3MJG8HlCgNlaZMzIDYP5kLOowj7 14G4MpXw5QH9f/PGw2U0fPHlOjPbfMcaeIltCmlqI6Dam+3ystos0DGPWPEudWh3DXoy OQFiQ/tZ4f+X5tEKW0hkK3yHUyAFr+kJydGvwL3Cup/76OyGZC1BEPnnbCvFc/QutqK5 7ZqT5bFLtiPHVjKOzeSUG/aInXnJVaxbHeyQjMsIQ3wcekLepzXyLPPGq190oLyY2G2T OhDJOpmVSuFAWxgpB/9sL6wJjHSr/pebeu2XU2tXzimcVTYNZwwehmdPue/hn4azlLbl 1xzw== X-Gm-Message-State: AFqh2kqEsZdYSVaorrKPQfYDrJ9Q2kUisZLHrIozZIrEopFUK2alLjGO LApA5atJg/0EolMq79nGsAUovrzNc7h0aTuw X-Received: by 2002:aca:d80b:0:b0:366:bfc6:4a78 with SMTP id p11-20020acad80b000000b00366bfc64a78mr19838189oig.20.1675061080625; Sun, 29 Jan 2023 22:44:40 -0800 (PST) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id o64-20020aca5a43000000b003670342726fsm4427883oib.12.2023.01.29.22.44.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 22:44:40 -0800 (PST) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Vincenzo Palazzo Subject: [PATCH v2 4/5] rust: types: implement `ForeignOwnable` for the unit type Date: Mon, 30 Jan 2023 03:44:03 -0300 Message-Id: <20230130064404.744600-4-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230130064404.744600-1-wedsonaf@gmail.com> References: <20230130064404.744600-1-wedsonaf@gmail.com> MIME-Version: 1.0 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 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?1756429025810371290?= X-GMAIL-MSGID: =?utf-8?q?1756429025810371290?= This allows us to use the unit type `()` when we have no object whose ownership must be managed but one implementing the `ForeignOwnable` trait is needed. Signed-off-by: Wedson Almeida Filho Reviewed-by: Vincenzo Palazzo Reviewed-by: Gary Guo Reviewed-by: Andreas Hindborg --- v1 -> v2: use `NonNull::dangling` to generate pointer rust/kernel/types.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 411655eca3e9..9d0fdbc55843 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -85,6 +85,18 @@ impl ForeignOwnable for Box { } } +impl ForeignOwnable for () { + type Borrowed<'a> = (); + + fn into_foreign(self) -> *const core::ffi::c_void { + core::ptr::NonNull::dangling().as_ptr() + } + + unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {} + + unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} +} + /// Runs a cleanup function/closure when dropped. /// /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running. From patchwork Mon Jan 30 06:44:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 50157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2039762wrn; Sun, 29 Jan 2023 22:51:45 -0800 (PST) X-Google-Smtp-Source: AK7set/YQW4e8h/lH0t373AEvdV81nZ9wto5ODnvxZL1yswGpszBdaEmbgnxZ380Ql6z88pJZdo7 X-Received: by 2002:a17:906:28c4:b0:882:2027:c8e2 with SMTP id p4-20020a17090628c400b008822027c8e2mr6727090ejd.56.1675061505057; Sun, 29 Jan 2023 22:51:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675061505; cv=none; d=google.com; s=arc-20160816; b=l90VApYUNQDA3sZuAiszviyHLLdF1sFHudTizesEuk3LlCFOLcrNGsFo/mqq6eQooL a5hPTtO1HW989zPUe6xoV/6bOAKe9D6OPJTKY/dM8u8u0VZtG0S2xp9UjAZfrfltW6EW Z+JjUr8EjsH4xmCIo0mCjLqUCste6S+W5/sgWnQNWOXRYR51nuhnCtiHuf/G8mYxz2dO 9trnk/NdBd04HBTte4Itd1N4JLCKQQ17g+5/YmKoW6npdEQVrA9xdMt7Z2wADYnoecMy 7i9VVIOkbp1mtal4iKvwSbCrE+d5WRXOmgxzKXTTWpaqvYXCKCON4n4IXazi6zNCouaP MXOA== 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=qQ9qgUpJKoZ5vFNL/Ds3EfhNmCcB+s3S4YIjtxQ6XEM=; b=HwqEXqkffwx9voDyphq0QWn1UyAOFrCIi8Eo6QcYBJ9pZgaBGmi2SAeAWvCGLLnLNh dgHymVkuNujlsIqsCfVaaNkvgCbWdGKvP4qVQD86SPK6L+x48mBkFVQHKYcNCHXEsd27 4yPGJBu8hpxUHy4ATPNyAhiRNiuduwCErE0ndqHjf9O9AEb+LvmH9XzEynFOeot43Qa+ +VUapLYGXdaH6IjuheJsNX/yKMbyw3b6NryBNeDjyDrir3FjhdIGyu80lFcMqHMNMn6O ga1iYrwpIKRy/Si3liWxzNXJru8MUnjv2a2HUr6J/b96Eyx1x37Q3bk3uS4LGSjHzwoO FG8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=CYBfd2h4; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id et4-20020a170907294400b00886e4a97694si3627669ejc.863.2023.01.29.22.51.22; Sun, 29 Jan 2023 22:51:45 -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=@gmail.com header.s=20210112 header.b=CYBfd2h4; 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 S235557AbjA3Gqq (ORCPT + 99 others); Mon, 30 Jan 2023 01:46:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235850AbjA3Gou (ORCPT ); Mon, 30 Jan 2023 01:44:50 -0500 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E061A27D6F; Sun, 29 Jan 2023 22:44:45 -0800 (PST) Received: by mail-oi1-x22c.google.com with SMTP id r9so9240877oig.12; Sun, 29 Jan 2023 22:44:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=qQ9qgUpJKoZ5vFNL/Ds3EfhNmCcB+s3S4YIjtxQ6XEM=; b=CYBfd2h4juLb8ljnjOPnGGQy975kHYuXZKKiex0EiXvVdjsCLTYYd8A1e8Zlzam45F YzlFB1FKHC5fK48nD2AWqSM+4J6FVykrjrEvIpiDw0HeTkQm+m8QSf6JPDOq+sOdz2Vk TF2+6e1unJq8erVP4qARMVkJ5YJpiHIozLI3Sf1VanWf1norVjiC+i1cJQY7wTvMptOa OmMQgcQDjpoqUrZ/2mbksTBeGldJflLL1VUalb6djHZylczS7bIZs7FZm5Z8/gadrjEj lHrxNC/mLW1BbhjONgH+x/0JchUC/If7sKtjMbXMtyxJ4Q+V9yNHQZ8f/r8QuCuxjjLe Xriw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=qQ9qgUpJKoZ5vFNL/Ds3EfhNmCcB+s3S4YIjtxQ6XEM=; b=4yFbdX94cKP3Q3NiWswb2raKWpbVjYQjTYx6rXo+I+ESl1dOLmqxTO7Yz9bdb+C5H6 dxJBIoAZO6x4s4MjjYtAcG0GfPkMDbbo+XhrJX0Mfv2ckwlSJEXAub2wUDT6gj830PNg 4GjW04dqb9NTgPYPa9jqmYk5eCebhxQRNl42n5t3RNqo6RUSZp/Lt4L3ayfF+BRn8JXi 9ABxmN/ly9CBY5+WJ8+rNCF1dPPjDZUxykEZL0+ZhhrDvkMgHn3D9pGn0pcI6j2Iawty 8zSx0k0HZL6fjbuHvkMRtQuAzJAdoOhRLUbrvK0Lg+9kxYa1jVSweJ4m53YpsreyrJqT 0RIA== X-Gm-Message-State: AO0yUKXCKPuUV9tLWKlazneo4f6iU2ijhz7kok8gGXTJnMH9MAGkEcEF zhNlhWQo6Cm9mynGOca8UqOjz1DqV4KeiiwB X-Received: by 2002:a05:6808:2394:b0:378:2aa6:ddcd with SMTP id bp20-20020a056808239400b003782aa6ddcdmr2723386oib.17.1675061085231; Sun, 29 Jan 2023 22:44:45 -0800 (PST) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id o64-20020aca5a43000000b003670342726fsm4427883oib.12.2023.01.29.22.44.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 22:44:45 -0800 (PST) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Vincenzo Palazzo , Alice Ferrazzi Subject: [PATCH v2 5/5] rust: types: implement `ForeignOwnable` for `Arc` Date: Mon, 30 Jan 2023 03:44:04 -0300 Message-Id: <20230130064404.744600-5-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230130064404.744600-1-wedsonaf@gmail.com> References: <20230130064404.744600-1-wedsonaf@gmail.com> MIME-Version: 1.0 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 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?1756429293087622593?= X-GMAIL-MSGID: =?utf-8?q?1756429293087622593?= This allows us to hand ownership of Rust ref-counted objects to the C side of the kernel. Signed-off-by: Wedson Almeida Filho Reviewed-by: Gary Guo Reviewed-by: Vincenzo Palazzo Reviewed-by: Alice Ferrazzi --- v1 -> v2: Unmodified rust/kernel/sync/arc.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index ff73f9240ca1..519a6ec43644 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -15,7 +15,11 @@ //! //! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html -use crate::{bindings, error::Result, types::Opaque}; +use crate::{ + bindings, + error::Result, + types::{ForeignOwnable, Opaque}, +}; use alloc::boxed::Box; use core::{ marker::{PhantomData, Unsize}, @@ -189,6 +193,32 @@ impl Arc { } } +impl ForeignOwnable for Arc { + type Borrowed<'a> = ArcBorrow<'a, T>; + + fn into_foreign(self) -> *const core::ffi::c_void { + ManuallyDrop::new(self).ptr.as_ptr() as _ + } + + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> { + // SAFETY: By the safety requirement of this function, we know that `ptr` came from + // a previous call to `Arc::into_foreign`. + let inner = NonNull::new(ptr as *mut ArcInner).unwrap(); + + // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive + // for the lifetime of the returned value. Additionally, the safety requirements of + // `ForeignOwnable::borrow_mut` ensure that no new mutable references are created. + unsafe { ArcBorrow::new(inner) } + } + + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + // SAFETY: By the safety requirement of this function, we know that `ptr` came from + // a previous call to `Arc::into_foreign`, which owned guarantees that `ptr` is valid and + // owns a reference. + unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) } + } +} + impl Deref for Arc { type Target = T;