All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Hindborg <nmi@metaspace.dk>
To: Asahi Lina <lina@asahilina.net>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	asahi@lists.linux.dev
Subject: Re: [PATCH 1/2] rust: sync: arc: implement Arc<dyn Any + Send + Sync>::downcast()
Date: Mon, 27 Feb 2023 12:46:45 +0100	[thread overview]
Message-ID: <87356rba8a.fsf@metaspace.dk> (raw)
In-Reply-To: <20230224-rust-arc-v1-1-568eea613a41@asahilina.net>


Asahi Lina <lina@asahilina.net> writes:

> 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
>
> Signed-off-by: Asahi Lina <lina@asahilina.net>
> ---

Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>

>  rust/kernel/sync/arc.rs | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index a94e303217c6..752bd7c4699e 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -22,6 +22,7 @@ use crate::{
>  };
>  use alloc::boxed::Box;
>  use core::{
> +    any::Any,
>      fmt,
>      marker::{PhantomData, Unsize},
>      mem::{ManuallyDrop, MaybeUninit},
> @@ -220,6 +221,27 @@ impl<T: 'static> ForeignOwnable for Arc<T> {
>      }
>  }
>  
> +impl Arc<dyn Any + Send + Sync> {
> +    /// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
> +    // Based on the Rust standard library implementation, ver 1.62.0, which is
> +    // Apache-2.0 OR MIT.
> +    pub fn downcast<T>(self) -> core::result::Result<Arc<T>, Self>
> +    where
> +        T: Any + Send + Sync,
> +    {
> +        if (*self).is::<T>() {
> +            // SAFETY: We have just checked that the type is correct, so we can cast the pointer.
> +            unsafe {
> +                let ptr = self.ptr.cast::<ArcInner<T>>();
> +                core::mem::forget(self);
> +                Ok(Arc::from_inner(ptr))
> +            }
> +        } else {
> +            Err(self)
> +        }
> +    }
> +}
> +
>  impl<T: ?Sized> Deref for Arc<T> {
>      type Target = T;


  parent reply	other threads:[~2023-02-27 11:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24  7:59 [PATCH 0/2] rust: sync: Arc: Any downcasting and assume_init() Asahi Lina
2023-02-24  7:59 ` [PATCH 1/2] rust: sync: arc: implement Arc<dyn Any + Send + Sync>::downcast() Asahi Lina
2023-02-24 14:34   ` Martin Rodriguez Reboredo
2023-02-25  0:43   ` Gary Guo
2023-02-27 11:46   ` Andreas Hindborg [this message]
2023-03-01 17:22   ` Vincenzo Palazzo
2023-02-24  7:59 ` [PATCH 2/2] rust: sync: arc: Add UniqueArc<MaybeUninit<T>::assume_init() Asahi Lina
2023-02-24 14:37   ` Martin Rodriguez Reboredo
2023-02-25  0:41   ` Gary Guo
2023-02-27 11:48   ` Andreas Hindborg
2023-03-01 17:23   ` Vincenzo Palazzo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87356rba8a.fsf@metaspace.dk \
    --to=nmi@metaspace.dk \
    --cc=alex.gaynor@gmail.com \
    --cc=asahi@lists.linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.