From nobody Wed Dec 17 21:42:55 2025 Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52CA0268FC0; Wed, 9 Apr 2025 14:51:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210288; cv=none; b=b4aLYzlenybeWQd2DvX0GZvHLMilwQaxCONPu1fvyRk/9vq4QFShwNx2NzqE0yG7lDWwSUh+hPw55Gpt6BKVsFtWayO6wJhXgnZhiTs1KOh7iWk6VfTPaSVdd45kguKyIDS93+OJZ3YIuiG57YCXWuIBceLm9ayYv5gQLAVVkPo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210288; c=relaxed/simple; bh=DFFkISZ+wsor0lGXgM1JwIFeqscLbz0j3F8wFkfSE04=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nfxlyR491RpFh4bWuQdb6zIOFK4OkqJsSEG7ewcEQa2A8PLwpH6sEqTHlwpGMc+um6Xyu7aSqH29RWKW2PFl98CEXGCL211uEA+oy3mmYnaPc3Ifdj1ygkatCEHmxqV+/DrnbkEir5mS++5tj0WeQZ0uVQ3AmvHvA/QVJPGYwIM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Dws3JxN4; arc=none smtp.client-ip=209.85.219.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Dws3JxN4" Received: by mail-qv1-f45.google.com with SMTP id 6a1803df08f44-6ecfbf1c7cbso115910456d6.2; Wed, 09 Apr 2025 07:51:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744210285; x=1744815085; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=z0ja/1pV62iEC0nEku9AQfzl6u3YciLHVA48FbywNX0=; b=Dws3JxN4YnaAoDVUe15pPltpw76qlaYvTnT/xwI1hHfPVDXUW4TKsIjs9PP+TDhUhO Uzywat8lcaEmyyuFwmsprE63EuO+BAfhsq7gwDYPdemSCXO+zhMLGZvKloO19i1jSlft VNmq2reRRMxc8PJ9WeiXdlOiGV1NOcdnmVrxSkFn7eyXJd41ywcB8Y0LQbOHTMfZ1Nw9 +AX2U/JobAjW1H3YUjfBkJyL6vXkiWDMW68ahxe9QwPe15YOd/IQUid/vI+SI/VgBa7Q kOOcZ7LAQG8O5YJrLHRcgRh8GNB2cKZX3Dqix5nr72/WPeBEmj5uyGw9I8eVrYnNBqaw GeaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744210285; x=1744815085; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z0ja/1pV62iEC0nEku9AQfzl6u3YciLHVA48FbywNX0=; b=GomDCVn+d2+WPYV2JQFn3aOV4Lf5hiqZSojBgkSzJqZGTbV05bl5Ca+Rjv2pSMInnj ym2EDR+qeBzwsRRTGg+OPITKeZrFvYI407R+HvEginnXQhXgsTXlw7wst5tq8IuNLAAl MwMo3EI8qv55shdIwL98pJYQ6KjAYNRKjm22gS93iZ87G1FeHIlw41uDLzDefaD98JzL 3aYQ5D2Iiy69Twdl/XksZLD72IBuWr7NBUOUKATwqKtUNud+Kbk03trnIDMZdOzkctzo bVloixoO2lKAxMytOSS5KTNAKM6ZS2SM/LpGE6VIWbmLGdO1MjJx5L1BZSMwR6vaLYZC 5/iw== X-Forwarded-Encrypted: i=1; AJvYcCU7N8XbKqw9/8VUG378eUu/TlH0XqbTU74BnGyi7YPvip6jXy7QILpFf9fD8QyRLml2esFTfcAq6o/WMjs=@vger.kernel.org, AJvYcCVRSG6GggeHmLO6wCMV5nQMGVqI8NNf7iL03kjJpCHdRNwzq/e3q3Dgad4x2NrgqgrwOsYRjpgZXOL/@vger.kernel.org X-Gm-Message-State: AOJu0YzZGYytL3kNwYyyodthHpkUDRxgEN35GfJYnsH/yq8Ywn+7bWcy O+FwZM2j08Pu3mKu834ATy44cl4Uqo5dUewFAjSSUCFbiwZWlciH X-Gm-Gg: ASbGncsQPXkKmoJiZUDFgyHIKhgtZyBMYsaMNkNlv6RwyNq8bdkQC37WjV/6sUIlI3v XeyQUxAPl5j730R28Me+Qs6nXiwpQwtjvklAFfgcNwk+JlB4e0gdc2iQjUM8dJgkskaeMiTMdMa t6iQJfdC+lXIzQW8qvce5hOd8Q2TLxEDUVj2IzGZF3jru6Kzj/KkLz2LmqaEMEYxzm3L9mRP+AU pFidkhZe+Arq6LzAhA3zN0zPZBFf6LoAIEA7YDe0GO2bpZ7vuJVRvle3KfrxaQiA9ESsLlVlRih 6VaB+fypUeRhKN00YjS7AksLf5iwtc8FYrEUJgynRW/0i5NG43qkLreXc39cKTqxaLpyewveoJa 2bZg3x8hsbb05KgUfsFX5mZ+O96cpg3+jz+I7BKrS57+J X-Google-Smtp-Source: AGHT+IEoqYByp8TMre1nNThTk+9dqK44uGM767j00gxMWA5q6hplDrtk8wvCSopcREYhktQu0jwyvA== X-Received: by 2002:a05:6214:5016:b0:6d4:1ea3:981d with SMTP id 6a1803df08f44-6f0dd812473mr32099266d6.43.1744210285131; Wed, 09 Apr 2025 07:51:25 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:3298]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6f0de95fa89sm8229876d6.6.2025.04.09.07.51.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Apr 2025 07:51:24 -0700 (PDT) From: Tamir Duberstein Date: Wed, 09 Apr 2025 10:51:18 -0400 Subject: [PATCH v2 1/4] rust: list: simplify macro capture Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250409-list-no-offset-v2-1-0bab7e3c9fd8@gmail.com> References: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> In-Reply-To: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Avoid manually capturing generics; use `ty` to capture the whole type instead. Signed-off-by: Tamir Duberstein Reviewed-by: Christian Schrefl --- rust/kernel/list/impl_list_item_mod.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl= _list_item_mod.rs index a0438537cee1..5ed66fdce953 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -43,7 +43,7 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut List= Links { macro_rules! impl_has_list_links { ($(impl$(<$($implarg:ident),*>)? HasListLinks$(<$id:tt>)? - for $self:ident $(<$($selfarg:ty),*>)? + for $self:ty { self$(.$field:ident)* } )*) =3D> {$( // SAFETY: The implementation of `raw_get_list_links` only compile= s if the field has the @@ -51,9 +51,7 @@ macro_rules! impl_has_list_links { // // The behavior of `raw_get_list_links` is not changed since the `= addr_of_mut!` macro is // equivalent to the pointer offset operation in the trait definit= ion. - unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)?= for - $self $(<$($selfarg),*>)? - { + unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)?= for $self { const OFFSET: usize =3D ::core::mem::offset_of!(Self, $($field= ).*) as usize; =20 #[inline] @@ -85,18 +83,14 @@ pub unsafe trait HasSelfPtr macro_rules! impl_has_list_links_self_ptr { ($(impl$({$($implarg:tt)*})? HasSelfPtr<$item_type:ty $(, $id:tt)?> - for $self:ident $(<$($selfarg:ty),*>)? + for $self:ty { self.$field:ident } )*) =3D> {$( // SAFETY: The implementation of `raw_get_list_links` only compile= s if the field has the // right type. - unsafe impl$(<$($implarg)*>)? $crate::list::HasSelfPtr<$item_type = $(, $id)?> for - $self $(<$($selfarg),*>)? - {} + unsafe impl$(<$($implarg)*>)? $crate::list::HasSelfPtr<$item_type = $(, $id)?> for $self {} =20 - unsafe impl$(<$($implarg)*>)? $crate::list::HasListLinks$(<$id>)? = for - $self $(<$($selfarg),*>)? - { + unsafe impl$(<$($implarg)*>)? $crate::list::HasListLinks$(<$id>)? = for $self { const OFFSET: usize =3D ::core::mem::offset_of!(Self, $field) = as usize; =20 #[inline] --=20 2.49.0 From nobody Wed Dec 17 21:42:55 2025 Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87D41269AE5; Wed, 9 Apr 2025 14:51:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210289; cv=none; b=J/Q7ksrSfGuH8ESRJ4+oqAJeNZl2BS3RlhNMLY01lay//eZvhPgK95OrXqXToAvUVRhdzJFYDsmcqzvUnPJ5Al1iwCaZaH4hQtazIdRxdtwskm/xUS7FSTkrvGfHcwmm1we3fLIxG/tMsLToYd5RGuNQV63Lp6hFjZvODOY2eug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210289; c=relaxed/simple; bh=cxq8SQPIttegAQ/VdXTVQPKMu7L6YbwbWuqPynYdF28=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CiC8cDqw/hn/jLHDw3f+7VcAjnXCLvGFc7d6DHBYoAXBOPcReqL9mQHziQ0SpnKI4DGNtUlwxclAnax7hxS8bQRm5nTpyMT4Lx9BpWiUbTBNnYtRT+sO1ENivXeFpm6XgCQOjytUgC6McbQ6leQuG56DQK65Jlte3A6t2ecUWPE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IZ7Qcxgv; arc=none smtp.client-ip=209.85.219.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IZ7Qcxgv" Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-6e8f6970326so66504436d6.0; Wed, 09 Apr 2025 07:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744210286; x=1744815086; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=7OzkWd7763iU7orIo6m7aRkDgbxfxBvtpXsSVHS4Bj8=; b=IZ7QcxgvfN2IlTU6XeZqK4+ZXl9JcYgRNbVG6Z1E8t8S438kz7d99ERAwOQifFcF2Z +smL2/A6fruj6O4pn0o0n7z4EiDIznC7D1Othm4qzhTKAdbswZ0Rg0BSXImItrSPmaAm 7tV2T5sY9zUz+TB4EJMILfELYVvnSrISzfttwoVVQ1SmxDgZlfXgAsPxOs/U430kxSeU PbFDMP+N8baiDw1Tgus9cK6C1a6ZbibBmGzguyfouFlozxNhfO10cU3w8OZ5N2+moQB8 1h+3ol2QDczWqo9zyXCQ8S0Z+L8nSEf/LyZfeXzv/42DxBTpvXS1YhHqRWE1+IaUUYMA BnZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744210286; x=1744815086; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7OzkWd7763iU7orIo6m7aRkDgbxfxBvtpXsSVHS4Bj8=; b=IywJ4wHJ1iLY5d/eGnwggY2TLesrzCpRmT0038lncfUrEX9Cjs9E//ZaWOszqevZnz myaJDLYbCULt9nQpTUOWgzj6ftW/iHJaED9vi5NqFaJ/opkxl2XGfcHHckypICoILlR6 QcIhPgeFVkqL8MAWF7rKx5M3O+CF9FQWQd5PGcsOiy9x4AuopqEkduPiMNaDjsNDghE9 0JEbzUO0zTQo1nKVr8raqsGUYgLFz/31jDGkC6rG9oPiDpVUgTb5qLUyftIfIuuW9Ive HuqhdOqYbEwnguAtZmb4jnyrGGRukJ7qmyo+u9W2JyKFb5VozUmaaNczwMoxzFkkF64u Gl9A== X-Forwarded-Encrypted: i=1; AJvYcCWKl5wi7RgWK/m0UUbZfb5y0KL97cbBKY/uUvTjSRGOThcU4GzgSF0er/Wo/1cehViwDaIkBqWT1X+T@vger.kernel.org, AJvYcCXP24iovXNVI4CHq9UZnJe01kunTaia0OhCh9EJEd9Cqys0UHM/qcTDDFqotvGZF0xbGrKvZSDDcKQeGZY=@vger.kernel.org X-Gm-Message-State: AOJu0YxkJXWs1akP4bOvgihZOz6o+xbycX6qydGtfsxYbdUZ69+iSYrM g7KB9I8CYX+LMDwWLcoxb6tEVtC/tAdwncxiA656LxvBwI2i32bS X-Gm-Gg: ASbGncsPrzbb637wmB+aAXr9QIkxYXjnnlRiftwG2Bts7UQq1HXZHX9cNdGp2/HcRPu E3FlMPbtGSGZeI8ZO4tfb9HxiYnUxYlGlKZ1t5kvx7oy1okFvZQYTRonmYAINnVnN0PLQLZ+PVR lM6Gkc8U39gh+nCEqI1QR8dsfw1Fc1riKF6aqJpXCdgMvIrjeetaDlQZWXldWq4WB9iOTG/yzLe Up/L7f57/0cVZ3pwcUKRl3BTQBweMoCJbP5Rv/DnYDkP6yAjjrqwlDG3G0RNLFxJCHVjlR8X0Vz AGP+bhe3++nHh8Oz+nzZNYcFjh2sKktgfocq1pDBNZN54VkDNlyg4zR5InTaPOu3AYILzE2ujcs 0wO7Vfrx7GlMYZ8JMGVS3gyOb7RHvcIvBUTDx0fhHZaUbHdfvOCsPTRQ= X-Google-Smtp-Source: AGHT+IFEgXOQQgDsZwCtINEDiVAbz67PVXNc0OsrH929ybBY44azL5LL2jW8hqyUIGciSgHdrSaxhw== X-Received: by 2002:a05:6214:19eb:b0:6e6:6599:edf6 with SMTP id 6a1803df08f44-6f0dbc6b8acmr52816466d6.34.1744210286195; Wed, 09 Apr 2025 07:51:26 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:3298]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6f0de95fa89sm8229876d6.6.2025.04.09.07.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Apr 2025 07:51:25 -0700 (PDT) From: Tamir Duberstein Date: Wed, 09 Apr 2025 10:51:19 -0400 Subject: [PATCH v2 2/4] rust: list: use consistent type parameter style Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250409-list-no-offset-v2-2-0bab7e3c9fd8@gmail.com> References: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> In-Reply-To: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Refer to the type parameters of `impl_has_list_links{,_self_ptr}!` by the same name used in `impl_list_item!`. Capture type parameters of `impl_list_item!` as `tt` using `{}` to match the style of all other macros that work with generics. Signed-off-by: Tamir Duberstein Reviewed-by: Christian Schrefl --- rust/kernel/list/impl_list_item_mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl= _list_item_mod.rs index 5ed66fdce953..49f5af12e796 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -41,7 +41,7 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut List= Links { /// Implements the [`HasListLinks`] trait for the given type. #[macro_export] macro_rules! impl_has_list_links { - ($(impl$(<$($implarg:ident),*>)? + ($(impl$({$($generics:tt)*})? HasListLinks$(<$id:tt>)? for $self:ty { self$(.$field:ident)* } @@ -51,7 +51,7 @@ macro_rules! impl_has_list_links { // // The behavior of `raw_get_list_links` is not changed since the `= addr_of_mut!` macro is // equivalent to the pointer offset operation in the trait definit= ion. - unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)?= for $self { + unsafe impl$(<$($generics)*>)? $crate::list::HasListLinks$(<$id>)?= for $self { const OFFSET: usize =3D ::core::mem::offset_of!(Self, $($field= ).*) as usize; =20 #[inline] @@ -81,16 +81,16 @@ pub unsafe trait HasSelfPtr /// Implements the [`HasListLinks`] and [`HasSelfPtr`] traits for the give= n type. #[macro_export] macro_rules! impl_has_list_links_self_ptr { - ($(impl$({$($implarg:tt)*})? + ($(impl$({$($generics:tt)*})? HasSelfPtr<$item_type:ty $(, $id:tt)?> for $self:ty { self.$field:ident } )*) =3D> {$( // SAFETY: The implementation of `raw_get_list_links` only compile= s if the field has the // right type. - unsafe impl$(<$($implarg)*>)? $crate::list::HasSelfPtr<$item_type = $(, $id)?> for $self {} + unsafe impl$(<$($generics)*>)? $crate::list::HasSelfPtr<$item_type= $(, $id)?> for $self {} =20 - unsafe impl$(<$($implarg)*>)? $crate::list::HasListLinks$(<$id>)? = for $self { + unsafe impl$(<$($generics)*>)? $crate::list::HasListLinks$(<$id>)?= for $self { const OFFSET: usize =3D ::core::mem::offset_of!(Self, $field) = as usize; =20 #[inline] --=20 2.49.0 From nobody Wed Dec 17 21:42:55 2025 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25EDB26A087; Wed, 9 Apr 2025 14:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210290; cv=none; b=ENKZi9VzJFKynQp6fvn+QMrWvMQbsJN7RSxMD3Klux8EIyEdYthEhYdmWhFW5LT779nZYhaj3CUPqqxNeVHx73kDaP5aPTHbD63IraQzflNH1hSZWL2JSfBMfkQYRZeBMtUTKNpDmUem/iWXuKbIZ2Vh3NyZarhFEcNwh6rdOtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210290; c=relaxed/simple; bh=I3LhX3RHJuENzMWjOlJ7uQw/9Iqe+uTziMBBRl5iv9Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fgs7ELBiF4G2JYQD8opqI+6JC3Md+CDoppim12/FrikR6qvYTqc/9Pwb/+pvCtXK6cNjd3jPqPPmrKKQ9OhVVQFwo74nn0UI1YNYOUQYyKdF3aZMGfxju/QPZZ8CkfDpedhFp8aUxRB+GRMr5j5Zg4HRUiQWL+v0egDXhjGidUU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=X8wIdtlx; arc=none smtp.client-ip=209.85.219.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="X8wIdtlx" Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-6e8f05acc13so76537126d6.2; Wed, 09 Apr 2025 07:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744210288; x=1744815088; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=R6KDpG33HnsnAHaUls9M6YMElozMNYMw1uYDLkzYs8I=; b=X8wIdtlxhuXQk95NRVha/rITRGjvWDnKdnxTRxOtnLs8ZxOX+jtYZcL53tYPRdqFsF tzp/ncHG7uUV7GX+ulF3t1/nnQM3CUVORlncKh1cXPK6/ZxbjF1+NTRTBJ4sRAty8906 8wrnXfRAXpj1wwMyeLk8gUzPsdAIkyXde23zhmKdqZAY120sHCdPWHAp6PgOrPxbHAaM 9Y/WSl5MFirzzU63VijCj2M9IORK9bRy5y6xSiaPU6sp+7p26Mp1yfNLMGSQD1y1v/Be K3Bp9CVzwDdZfLwRNIBvAUKiGRB/rWZUN876BE7xCsgl+xC5grrsPDGIktSybVAB1f19 cWzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744210288; x=1744815088; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=R6KDpG33HnsnAHaUls9M6YMElozMNYMw1uYDLkzYs8I=; b=YL0Yc4OmUsKFHgD04UpGgp4nveObcoVKUkCSy0ZxbWQRfGSWZWSgrID3ROQbLHspy0 Ht9CFf+GZNG05Zy6p/SCRIqD4gfQoNNQa3KyGKixxzpohgWDiM1M/CLLk0uLIg3gMbfl eY9fMQLHX2d2w4tjyvwkN5ycW5tFjQilD3BYWezw0iqxgde8FTq2AZIF0b6PMn5ROgou nO612iONK9cvV4/d5jtCaDzFFYPHtV4GauKQHrudEC+pn7fa8ZrPFgV6gBtpEtKqjphJ 1AeLFy/8A9TPSI6zkpkSH0nrmfGwbfRYDSBrj37mQAxg61SxBzcFwhT8OSs9p34hVK4o 0iHg== X-Forwarded-Encrypted: i=1; AJvYcCVrm72LVdheXMhlTbzGY5OnOnWePpzAxRdG3O4o387va8xnflxEdjutWg4WmEmA3Ilb33Ig2Ct2es7i@vger.kernel.org, AJvYcCVtj8Wp+HnxVArameiKHatdURuyCNHGlOksY5QyjW5zeON8UwQpKP3/ekmL6yBqGxOfEYjG9sC4KUva+r4=@vger.kernel.org X-Gm-Message-State: AOJu0YwsB7FE3pQrS+6uh860xBBpIWkUyypoSacW9sn05SwTekXwjZx4 mY5f5y8dzNY8Sqs5VN1PtD7MRNmtiOLpJGYYHoCeV0ktKu6VrhWa X-Gm-Gg: ASbGncu9liCAz8auY93Vm6eSFPXZiF3zK6Ysw8SxErGGz98v4L+t2OnlexI74XNguyG cD2MP5I+MDEk0Cbxyf+EIjFg5UQXy+JX96GM+nHxQsRAfjuS9swQVQClNo6jtHdXLeL/S8RfBsM vAOQaHKENTVgC3hJbF1y9uj/FAZlcXEbbunlxxSN6347gxeEjbYjKNuAY/dw4SzHtelMQXHQbFf /iH1wbwKDd12ptxfWvMWrlk7s1oUaoqtR7F1eLphCKxuY+PN971ZyJOd70HIGJBNX/TZa7Wi8B9 B8JF7t+w5moVxgojPcrMfu8UI2C+iOkydZM2dQDP92E7LAmrsJIjRNGg9IukDzz/H8fevMqeZyo 0HWtokiHOzCBC77Nv7INrNripcuVMT9mCEDSaFTlQZCDF X-Google-Smtp-Source: AGHT+IEj6a/gTtE+8rEv6HGebDehanM2D4q9D2jv4HBP6KvgQF0IBOofx/wC7IRXnPFtdnSjPyqYbg== X-Received: by 2002:a05:6214:2343:b0:6e8:f3ec:5406 with SMTP id 6a1803df08f44-6f0dd0a3a40mr43823346d6.19.1744210287776; Wed, 09 Apr 2025 07:51:27 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:3298]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6f0de95fa89sm8229876d6.6.2025.04.09.07.51.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Apr 2025 07:51:26 -0700 (PDT) From: Tamir Duberstein Date: Wed, 09 Apr 2025 10:51:20 -0400 Subject: [PATCH v2 3/4] rust: list: use consistent self parameter name Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250409-list-no-offset-v2-3-0bab7e3c9fd8@gmail.com> References: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> In-Reply-To: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Refer to the self parameter of `impl_list_item!` by the same name used in `impl_has_list_links{,_self_ptr}!`. Signed-off-by: Tamir Duberstein Reviewed-by: Christian Schrefl --- rust/kernel/list/impl_list_item_mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl= _list_item_mod.rs index 49f5af12e796..d79bebb363ce 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -114,12 +114,12 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut = $crate::list::ListLinks$(<$ #[macro_export] macro_rules! impl_list_item { ( - $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { using ListLinks; })* ) =3D> {$( // SAFETY: See GUARANTEES comment on each method. - unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $t= { + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $s= elf { // GUARANTEES: // * This returns the same pointer as `prepare_to_insert` beca= use `prepare_to_insert` // is implemented in terms of `view_links`. @@ -178,12 +178,12 @@ unsafe fn post_remove(me: *mut $crate::list::ListLink= s<$num>) -> *const Self { )*}; =20 ( - $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { using ListLinksSelfPtr; })* ) =3D> {$( // SAFETY: See GUARANTEES comment on each method. - unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $t= { + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $s= elf { // GUARANTEES: // This implementation of `ListItem` will not give out exclusi= ve access to the same // `ListLinks` several times because calls to `prepare_to_inse= rt` and `post_remove` --=20 2.49.0 From nobody Wed Dec 17 21:42:55 2025 Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C702D27702E; Wed, 9 Apr 2025 14:51:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210292; cv=none; b=BTRXXzw3Mo3UA8WJOXDrMRM2eiy5mzVwvCvHbPO52PUfwgj64YUKUaQ1Wfl/HoH/a3OPSOMDBYpjbCHrcdGKF+vBSm4jcW9RUaUZXysEmBXq4QTbbmM2uWh++61BWodkbkPesgLxrrnR3kOrLS7VBIPJhJNmK4BazHcBIy0pixc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744210292; c=relaxed/simple; bh=QAraSClmc7xPwjtsN9qdACIwGCQfVwxdg6lXrbuDb0c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KrbMQEISn3ZXUWHAz+w3DLYdcOmdi3SosGDDEWxcDmxI7MQZxxKyPq8IbanjsotRR6GqEZcALLQM639SjOtTpy54ueAcnWlnTQgJ1shcLTY5JD5rHZW93D6wP5Fv3jT3TkbPmXvw6iqQ+mNB5s6QaM6z6oSYqdz1j7BKH9nyxxw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iGvoNRIz; arc=none smtp.client-ip=209.85.219.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iGvoNRIz" Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-6eaf1b6ce9aso75505406d6.2; Wed, 09 Apr 2025 07:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744210289; x=1744815089; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=mX/zVGLacBNtzH1Co9YrakQoJNZpEVypxiF7+Yiskio=; b=iGvoNRIzLA22yzh3pnz2Jb1ZN/cElLBRZHKpwCUjlh+LqjvtphXg0zyZQvYzJfa1tE LuOq0Bs0SotKw+TMFUpYDICoHq1n8Lv5ZRj7xevzLOR6t7s15uzWEI8LnR0BEodsUpdx 6EWLXnX+zYyQ1TcwPf61mXLeALkwlrGUTjwkBe/KyanBCjeSzpkTl8JhSVCsIasSYnaY WEDhs2U4pOeXW0Zn6mIaAtzM5HxBgAZD3STDuAy/DgeBZC3ObRYuLnmdV/5qxkX5/w1h L2zQeS65KwkLwiphEU2hihqwYq+ZLg7eo/LtdXYL0BmchQCwlDQqCSZ5xIBLjTMG/bD4 UxAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744210289; x=1744815089; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mX/zVGLacBNtzH1Co9YrakQoJNZpEVypxiF7+Yiskio=; b=F3Rv4LchS+ui7AmPXrMIf65vyBUYYSSgq54i01LETie/pzRQ1fBdSnjGhDMqMo43sE s4fHHJXli3rro6bbp5wK/wlOTlDmKIVSxucj7dIciIq80Ypn7q8tvx/c24Lnxxuu/oUc wUVaJhQve1gjo++OrwCXwEoORfttk7m4mH7w9f7SxPqxSZWCn7SSERMKtwnfwo/kVWNU XBsEktPfPysYpilC7oOh7Idhbvq8VmtT6Y33RpKirsr7ekfbpSt+enFGPz6F8GKLbWpN 9Mk72orhNrB9f8QSwEq9RD2mT8e4LqXeTSEbC7Ox+QyJ1e4pp2vzsFdUBavPGsf5ifT2 x5YQ== X-Forwarded-Encrypted: i=1; AJvYcCUoOjPpq/lvwCy2YQYzejXUTHEFerT6tWayRKBQHeYDmXHszJ5Ggkr/4YClp8BUe6JUsuzqhM4yRMeS1oQ=@vger.kernel.org, AJvYcCXx+TPkqq81a/S4B2EYjMKQSupamXVelNuSE9iS9wYUgaGD1nbgQNqi2hzXm8ZhGtnmy3slGGZ5vPva@vger.kernel.org X-Gm-Message-State: AOJu0Ywmkk8NZLEVFTaO6EorGOnjI4MuLEdfa3+VGGsCn1nBrC5XBn3W 6FxdBLo22udGfvCgzanvt9x3lB3ucgtMcoLQicmPr6gj/pB+rqmu X-Gm-Gg: ASbGncvIh8/rN0FF23RvC/z6UZMKlMlYaD4W4ivgBbd0nfeayqpOQyGpMidDOHVaOtJ XyFL3h2huSguo+We5JduNdF4MWuJwBawURef6ec+jUIE94spmraPSfjxQXcrxjNieVbhJv8jOy6 4ndSBPiXSnys55iiE8dQnfRsTlCZ6DKIjQjCvwLB/Cq/BCeQe8BXmWha5QJGTYRkcsq5tKI9xlY QSSbAuFCVFaHvctebnC6dXhX7+Py1YVh+4m/eY7vQIoZI1f9dl+jldeYMXlTnUEL2KujuZ5g2RB BilSshPsnwiLpxybm/GM1gkiW9yY9Q0/IUnQKHtAxmyeRhysxqqkLLpOnkprwYq+BVIihBLfMw9 T/I0QUkPg8hAb3ssWT/Mn789xKOhtlUwP49FRT0K4eLxFUnSA78WW12M= X-Google-Smtp-Source: AGHT+IH2vXb0WkovZiy7D9/YH7RRtFDAXGJMU4MkwyF8t1LWoxRX7ev+T3Umg5SjxUjsTmpbsIFvIg== X-Received: by 2002:a05:6214:19eb:b0:6e6:6599:edf6 with SMTP id 6a1803df08f44-6f0dbc6b8acmr52819376d6.34.1744210289334; Wed, 09 Apr 2025 07:51:29 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:3298]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6f0de95fa89sm8229876d6.6.2025.04.09.07.51.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Apr 2025 07:51:28 -0700 (PDT) From: Tamir Duberstein Date: Wed, 09 Apr 2025 10:51:21 -0400 Subject: [PATCH v2 4/4] rust: list: remove OFFSET constants Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250409-list-no-offset-v2-4-0bab7e3c9fd8@gmail.com> References: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> In-Reply-To: <20250409-list-no-offset-v2-0-0bab7e3c9fd8@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Replace `ListLinksSelfPtr::LIST_LINKS_SELF_PTR_OFFSET` with `unsafe fn raw_get_self_ptr` which returns a pointer to the field rather than requiring the caller to do pointer arithmetic. Implement `HasListLinks::raw_get_list_links` in `impl_has_list_links!`, narrowing the interface of `HasListLinks` and replacing pointer arithmetic with `container_of!`. Modify `impl_list_item` to also invoke `impl_has_list_links!` or `impl_has_list_links_self_ptr!`. This is necessary to allow `impl_list_item` to see more of the tokens used by `impl_has_list_links{,_self_ptr}!`. A similar API change was discussed on the hrtimer series[1]. Link: https://lore.kernel.org/all/20250224-hrtimer-v3-v6-12-rc2-v9-1-5bd3bf= 0ce6cc@kernel.org/ [1] Signed-off-by: Tamir Duberstein --- rust/kernel/list.rs | 18 +++--- rust/kernel/list/impl_list_item_mod.rs | 100 +++++++++++++++--------------= ---- 2 files changed, 56 insertions(+), 62 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index a335c3b1ff5e..f370a8c1df98 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -212,9 +212,6 @@ unsafe impl Send for L= istLinksSelfPtr {} unsafe impl Sync for ListLinksSelfPtr {} =20 impl ListLinksSelfPtr { - /// The offset from the [`ListLinks`] to the self pointer field. - pub const LIST_LINKS_SELF_PTR_OFFSET: usize =3D core::mem::offset_of!(= Self, self_ptr); - /// Creates a new initializer for this type. pub fn new() -> impl PinInit { // INVARIANT: Pin-init initializers can't be used on an existing `= Arc`, so this value will @@ -229,6 +226,16 @@ pub fn new() -> impl PinInit { self_ptr: Opaque::uninit(), } } + + /// Returns a pointer to the self pointer. + /// + /// # Safety + /// + /// The provided pointer must point at a valid struct of type `Self`. + pub unsafe fn raw_get_self_ptr(me: *mut Self) -> *const Opaque<*const = T> { + // SAFETY: The caller promises that the pointer is valid. + unsafe { ptr::addr_of!((*me).self_ptr) } + } } =20 impl, const ID: u64> List { @@ -603,14 +610,11 @@ fn next(&mut self) -> Option> { /// } /// } /// -/// kernel::list::impl_has_list_links! { -/// impl HasListLinks<0> for ListItem { self.links } -/// } /// kernel::list::impl_list_arc_safe! { /// impl ListArcSafe<0> for ListItem { untracked; } /// } /// kernel::list::impl_list_item! { -/// impl ListItem<0> for ListItem { using ListLinks; } +/// impl ListItem<0> for ListItem { using ListLinks { self.links }; } /// } /// /// // Use a cursor to remove the first element with the given value. diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl= _list_item_mod.rs index d79bebb363ce..24489996d11b 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -6,21 +6,18 @@ =20 use crate::list::ListLinks; =20 -/// Declares that this type has a `ListLinks` field at a fixed offset. +/// Declares that this type has a [`ListLinks`] field. /// -/// This trait is only used to help implement `ListItem` safely. If `ListI= tem` is implemented +/// This trait is only used to help implement [`ListItem`] safely. If [`Li= stItem`] is implemented /// manually, then this trait is not needed. Use the [`impl_has_list_links= !`] macro to implement /// this trait. /// /// # Safety /// -/// All values of this type must have a `ListLinks` field at the given= offset. +/// The methods on this trait must have exactly the behavior that the defi= nitions given below have. /// -/// The behavior of `raw_get_list_links` must not be changed. +/// [`ListItem`]: crate::list::ListItem pub unsafe trait HasListLinks { - /// The offset of the `ListLinks` field. - const OFFSET: usize; - /// Returns a pointer to the [`ListLinks`] field. /// /// # Safety @@ -28,14 +25,7 @@ pub unsafe trait HasListLinks { /// The provided pointer must point at a valid struct of type `Self`. /// /// [`ListLinks`]: ListLinks - // We don't really need this method, but it's necessary for the implem= entation of - // `impl_has_list_links!` to be correct. - #[inline] - unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks { - // SAFETY: The caller promises that the pointer is valid. The impl= ementer promises that the - // `OFFSET` constant is correct. - unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut ListLinks } - } + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks; } =20 /// Implements the [`HasListLinks`] trait for the given type. @@ -48,14 +38,11 @@ macro_rules! impl_has_list_links { )*) =3D> {$( // SAFETY: The implementation of `raw_get_list_links` only compile= s if the field has the // right type. - // - // The behavior of `raw_get_list_links` is not changed since the `= addr_of_mut!` macro is - // equivalent to the pointer offset operation in the trait definit= ion. unsafe impl$(<$($generics)*>)? $crate::list::HasListLinks$(<$id>)?= for $self { - const OFFSET: usize =3D ::core::mem::offset_of!(Self, $($field= ).*) as usize; - #[inline] unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::l= ist::ListLinks$(<$id>)? { + const _: usize =3D ::core::mem::offset_of!($self, $($field= ).*); + // SAFETY: The caller promises that the pointer is not dan= gling. We know that this // expression doesn't follow any pointers, as the `offset_= of!` invocation above // would otherwise not compile. @@ -66,12 +53,15 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $c= rate::list::ListLinks$(<$ } pub use impl_has_list_links; =20 -/// Declares that the `ListLinks` field in this struct is inside a `Li= stLinksSelfPtr`. +/// Declares that the [`ListLinks`] field in this struct is inside a +/// [`ListLinksSelfPtr`]. /// /// # Safety /// -/// The `ListLinks` field of this struct at the offset `HasListLinks::OFFSET` must be -/// inside a `ListLinksSelfPtr`. +/// The [`ListLinks`] field of this struct at [`HasListLinks::raw_= get_list_links`] must be +/// inside a [`ListLinksSelfPtr`]. +/// +/// [`ListLinksSelfPtr`]: crate::list::ListLinksSelfPtr pub unsafe trait HasSelfPtr where Self: HasListLinks, @@ -91,8 +81,6 @@ macro_rules! impl_has_list_links_self_ptr { unsafe impl$(<$($generics)*>)? $crate::list::HasSelfPtr<$item_type= $(, $id)?> for $self {} =20 unsafe impl$(<$($generics)*>)? $crate::list::HasListLinks$(<$id>)?= for $self { - const OFFSET: usize =3D ::core::mem::offset_of!(Self, $field) = as usize; - #[inline] unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::l= ist::ListLinks$(<$id>)? { // SAFETY: The caller promises that the pointer is not dan= gling. @@ -115,9 +103,13 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $= crate::list::ListLinks$(<$ macro_rules! impl_list_item { ( $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { - using ListLinks; + using ListLinks { self$(.$field:ident)* }; })* ) =3D> {$( + $crate::list::impl_has_list_links! { + impl$({$($generics:tt)*})? HasListLinks<$num> for $self { self= $(.$field)* } + } + // SAFETY: See GUARANTEES comment on each method. unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $s= elf { // GUARANTEES: @@ -133,20 +125,19 @@ unsafe fn view_links(me: *const Self) -> *mut $crate:= :list::ListLinks<$num> { } =20 // GUARANTEES: - // * `me` originates from the most recent call to `prepare_to_= insert`, which just added - // `offset` to the pointer passed to `prepare_to_insert`. Th= is method subtracts - // `offset` from `me` so it returns the pointer originally p= assed to - // `prepare_to_insert`. + // * `me` originates from the most recent call to `prepare_to_= insert`, which calls + // `raw_get_list_link`, which is implemented using `addr_of_= mut!((*self).$field)`. + // This method uses `container_of` to perform the inverse op= eration, so it returns the + // pointer originally passed to `prepare_to_insert`. // * The pointer remains valid until the next call to `post_re= move` because the caller // of the most recent call to `prepare_to_insert` promised t= o retain ownership of the // `ListArc` containing `Self` until the next call to `post_= remove`. The value cannot // be destroyed while a `ListArc` reference exists. unsafe fn view_value(me: *mut $crate::list::ListLinks<$num>) -= > *const Self { - let offset =3D >:= :OFFSET; // SAFETY: `me` originates from the most recent call to `p= repare_to_insert`, so it - // points at the field at offset `offset` in a value of ty= pe `Self`. Thus, - // subtracting `offset` from `me` is still in-bounds of th= e allocation. - unsafe { (me as *const u8).sub(offset) as *const Self } + // points at the field `$field` in a value of type `Self`.= Thus, reversing that + // operation is still in-bounds of the allocation. + $crate::container_of!(me, Self, $($field)*) } =20 // GUARANTEES: @@ -163,25 +154,28 @@ unsafe fn prepare_to_insert(me: *const Self) -> *mut = $crate::list::ListLinks<$nu } =20 // GUARANTEES: - // * `me` originates from the most recent call to `prepare_to_= insert`, which just added - // `offset` to the pointer passed to `prepare_to_insert`. Th= is method subtracts - // `offset` from `me` so it returns the pointer originally p= assed to - // `prepare_to_insert`. + // * `me` originates from the most recent call to `prepare_to_= insert`, which calls + // `raw_get_list_link`, which is implemented using `addr_of_= mut!((*self).$field)`. + // This method uses `container_of` to perform the inverse op= eration, so it returns the + // pointer originally passed to `prepare_to_insert`. unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>) = -> *const Self { - let offset =3D >:= :OFFSET; // SAFETY: `me` originates from the most recent call to `p= repare_to_insert`, so it - // points at the field at offset `offset` in a value of ty= pe `Self`. Thus, - // subtracting `offset` from `me` is still in-bounds of th= e allocation. - unsafe { (me as *const u8).sub(offset) as *const Self } + // points at the field `$field` in a value of type `Self`.= Thus, reversing that + // operation is still in-bounds of the allocation. + $crate::container_of!(me, Self, $($field)*) } } )*}; =20 ( $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { - using ListLinksSelfPtr; + using ListLinksSelfPtr { self$(.$field:ident)* }; })* ) =3D> {$( + $crate::list::impl_has_list_links_self_ptr! { + impl$({$($generics:tt)*})? HasListLinks<$num> for $self { self= $(.$field)* } + } + // SAFETY: See GUARANTEES comment on each method. unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $s= elf { // GUARANTEES: @@ -196,13 +190,10 @@ unsafe fn prepare_to_insert(me: *const Self) -> *mut = $crate::list::ListLinks<$nu // SAFETY: The caller promises that `me` points at a valid= value of type `Self`. let links_field =3D unsafe { >::view_links(me) }; =20 - let spoff =3D $crate::list::ListLinksSelfPtr::= ::LIST_LINKS_SELF_PTR_OFFSET; - // Goes via the offset as the field is private. - // - // SAFETY: The constant is equal to `offset_of!(ListLinksS= elfPtr, self_ptr)`, so - // the pointer stays in bounds of the allocation. - let self_ptr =3D unsafe { (links_field as *const u8).add(s= poff) } - as *const $crate::types::Opaque<*const Self>; + // SAFETY: By the same reasoning above, `links_field` is a= valid pointer. + let self_ptr =3D unsafe { + $crate::list::ListLinksSelfPtr::::raw_get_= self_ptr(links_field) + }; let cell_inner =3D $crate::types::Opaque::raw_get(self_ptr= ); =20 // SAFETY: This value is not accessed in any other places = than `prepare_to_insert`, @@ -241,11 +232,10 @@ unsafe fn view_links(me: *const Self) -> *mut $crate:= :list::ListLinks<$num> { // `ListArc` containing `Self` until the next call to `post_= remove`. The value cannot // be destroyed while a `ListArc` reference exists. unsafe fn view_value(links_field: *mut $crate::list::ListLinks= <$num>) -> *const Self { - let spoff =3D $crate::list::ListLinksSelfPtr::= ::LIST_LINKS_SELF_PTR_OFFSET; - // SAFETY: The constant is equal to `offset_of!(ListLinksS= elfPtr, self_ptr)`, so - // the pointer stays in bounds of the allocation. - let self_ptr =3D unsafe { (links_field as *const u8).add(s= poff) } - as *const ::core::cell::UnsafeCell<*const Self>; + // SAFETY: By the same reasoning above, `links_field` is a= valid pointer. + let self_ptr =3D unsafe { + $crate::list::ListLinksSelfPtr::::raw_get_= self_ptr(links_field) + }; let cell_inner =3D ::core::cell::UnsafeCell::raw_get(self_= ptr); // SAFETY: This is not a data race, because the only funct= ion that writes to this // value is `prepare_to_insert`, but by the safety require= ments the --=20 2.49.0