From nobody Tue Sep 9 17:14:06 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2599C76196 for ; Mon, 3 Apr 2023 10:02:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232176AbjDCKCO (ORCPT ); Mon, 3 Apr 2023 06:02:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232157AbjDCKBv (ORCPT ); Mon, 3 Apr 2023 06:01:51 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF5CD83F0; Mon, 3 Apr 2023 03:01:45 -0700 (PDT) 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 D8E5142118; Mon, 3 Apr 2023 10:01:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680516104; bh=mt96Ry31EMWGGv3xrCXuVnmwVeuczytGDrnki4wvMRY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=PqipZl7AWWAdCe5Yzxssc80eVykz40viy3qcLHZ62UHBx/iPgJuhXz2D79idamOaI NjMH16vDg2ZOGI62I+F7GkXLjwbkPmSUFgbQ0tlddmi97ND25mlUd9mjqoQZvkVJMW 5lNKqqbWg6Tew9DZcNqgVPjsnW8yZNbOB/gHGKAMjc3WjD8lPyIwO+791nJ+sw2g4G 0zFxXfk6ga0yVxkWVRqkGOdZGPxC34X890SYtMogMLy1bT9VEMokw0SLpZB3X8EWCn XG4avnkZjk6wl5W3+ReAjnFCMNjIAIDYyb2xHMp2sBE9CQIg6fKpiB4W9rkzkWOaSE hrlEZ6LzmpRRg== From: Asahi Lina Date: Mon, 03 Apr 2023 19:01:11 +0900 Subject: [PATCH v2 1/2] rust: sync: arc: Implement Arc::downcast() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-arc-v2-1-5c97a865b276@asahilina.net> References: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> In-Reply-To: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: Andreas Hindborg , Vincenzo Palazzo , Martin Rodriguez Reboredo , Benno Lossin , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680516096; l=2373; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=mt96Ry31EMWGGv3xrCXuVnmwVeuczytGDrnki4wvMRY=; b=CZTlhuH+UKIX+dfSA8mpbpmUrwOHpedGgcAWg9ihdc3Zse8Z/baeEqCP51bezEj85mQaJMvmE r9NNAIqSPQZA7JYdmy2VtJ2yQlX+qirhipwZ+r9tGnKWWX4oAUrDI9y X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This mirrors the standard library's alloc::sync::Arc::downcast(). Based on the Rust standard library implementation, ver 1.62.0, licensed under "Apache-2.0 OR MIT", from: https://github.com/rust-lang/rust/tree/1.62.0/library/alloc/src For copyright details, please see: https://github.com/rust-lang/rust/blob/1.62.0/COPYRIGHT Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Andreas Hindborg Reviewed-by: Vincenzo Palazzo Reviewed-by: Gary Guo Signed-off-by: Asahi Lina --- rust/kernel/sync/arc.rs | 2 ++ rust/kernel/sync/arc/std_vendor.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index f2f1c83d72ba..b904ed898d3a 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -522,3 +522,5 @@ impl DerefMut for UniqueArc { unsafe { &mut self.inner.ptr.as_mut().data } } } + +mod std_vendor; diff --git a/rust/kernel/sync/arc/std_vendor.rs b/rust/kernel/sync/arc/std_= vendor.rs new file mode 100644 index 000000000000..a66a0c2831b3 --- /dev/null +++ b/rust/kernel/sync/arc/std_vendor.rs @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! The contents of this file come from the Rust standard library, hosted = in +//! the repository, licensed under +//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, +//! see . + +use crate::sync::{arc::ArcInner, Arc}; +use core::any::Any; + +impl Arc { + /// Attempt to downcast the `Arc` to a concrete= type. + pub fn downcast(self) -> core::result::Result, Self> + where + T: Any + Send + Sync, + { + if (*self).is::() { + // SAFETY: We have just checked that the type is correct, so w= e can cast the pointer. + unsafe { + let ptr =3D self.ptr.cast::>(); + core::mem::forget(self); + Ok(Arc::from_inner(ptr)) + } + } else { + Err(self) + } + } +} --=20 2.40.0