From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADC292135B1; Mon, 3 Mar 2025 14:10:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741011011; cv=none; b=DOvpjBpV737ZRqoBt7Rvy/wv8DUOstspRQzNoyd/8KsakOMyJIeVD1O+h/5+B5V4VjPVYp03N8tWLa3kja/9Amwpqo7OxrsorS0JLLwFuDynQHU3GtPIf6e0XHiU6ufsTB4Vo/l4VtNSMIFfEhtkvqx1KAS+pGWF/GXygH+8pIE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741011011; c=relaxed/simple; bh=4Y9h9Junw2qorQN9fokN1ASjhbtEC6V0icdZP7DXRU4=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=TnBxA9FguTvomXfcfwQCF1frPI/QFvI45k0T+9HHmgr168oUxDFk/457cqJapRjzjvH3caO3EkOAjfpBKi9ksHuUsANzwYPKQLQgzICa8Dc6x+6cMZh9E3zctEx97rah1qQJ5Wq0IcmOWGn6lk53uG9NOiEmBj9doAKb385fpuY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TSwX0+eP; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TSwX0+eP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AD59C4CED6; Mon, 3 Mar 2025 14:10:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741011009; bh=4Y9h9Junw2qorQN9fokN1ASjhbtEC6V0icdZP7DXRU4=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=TSwX0+eP8YJl/OTJ6QsFkoj29zeXZf0wTJ37LlGamFmF0G5qoK2VMOPais4cYGbAa bHKhHb5K+cI5pC2bQbYFZErKe6ev/RFyAqivqB3HZ9oGACyWcuDSiuKx8kyd0K6UHx 5kt5SqgvbMj74XWnDtRYPf1MMZEewaCTI3at7gvbHv2BnUKvKZt58wbeLgQt+H1IfF 609jXDfA3aC+eSwQc1SBAxS2LNNiaOR0ncLOwo7TZV0jYLh0wqUebgLHxJx6oaqwg/ bV8UmA1wTBOZiY5DydDqSVOUMms6Y3O9WP3bQ+JSktwUUdwRJUxhCM9YNg7SBSoyIs 41eXT969uouMw== From: Andreas Hindborg To: "Oliver Mangold" Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , , Subject: Re: [PATCH v2] rust: adding UniqueRefCounted and UniqueRef types In-Reply-To: (Oliver Mangold's message of "Fri, 28 Feb 2025 18:01:55 +0000") References: User-Agent: mu4e 1.12.7; emacs 29.4 Date: Mon, 03 Mar 2025 15:09:59 +0100 Message-ID: <87zfi2jjd4.fsf@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain "Oliver Mangold" writes: [...] > +/// Types that are [`AlwaysRefCounted`] and can be safely converted to an [`UniqueRef`] > +/// > +/// # Safety > +/// > +/// Implementers must ensure that the methods of the trait > +/// change the reference count of the underlying object such that: > +/// - the uniqueness invariant is upheld, i.e. it is not possible > +/// to obtain another reference by any means (other than through the [`UniqueRef`]) > +/// until the [`UniqueRef`] is dropped or converted to an [`ARef`]. > +/// - [`UniqueRefCounted::dec_ref`] correctly frees the underlying object. > +/// - [`UniqueRefCounted::unique_to_shared`] set the reference count to the value > +/// - that the returned [`ARef`] expects for an object with a single reference > +/// in existence. > +pub unsafe trait UniqueRefCounted: AlwaysRefCounted + Sized { > + /// Checks if the [`ARef`] is unique and convert it > + /// to an [`UniqueRef`] it that is that case. > + /// Otherwise it returns again an [`ARef`] to the same > + /// underlying object. > + fn try_shared_to_unique(this: ARef) -> Result, ARef>; This could just be `try_into_unique`, since the type of `this` gives the rest of the context. > + /// Converts the [`UniqueRef`] into an [`ARef`]. > + fn unique_to_shared(this: UniqueRef) -> ARef; Similarly, this could be `into_shared`. Best regards, Andreas Hindborg