Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3180509rwb; Fri, 16 Dec 2022 11:07:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf4vJ0nmHqhbdAn6ZE/aG1ztfq8gp5vcqAg04g9OyEhpxvr1it251tyQNrrLyfyubCCgCtXZ X-Received: by 2002:a17:902:f785:b0:188:b800:3335 with SMTP id q5-20020a170902f78500b00188b8003335mr38095497pln.67.1671217671570; Fri, 16 Dec 2022 11:07:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671217671; cv=none; d=google.com; s=arc-20160816; b=weMg3W3pn0/FyQc4dWKxDS4bfWKQg7Payroy5q9ssRzoZKx+ZA99sgWsl1sTtCQvZW lDLCKI4D6dd0gSRWb82QARFOFgSCjH3MFwpzNrqOfsiD75Qz8VdWRZv9p6l1NKHsqpR+ kAcHSUKbY/Q5F2mh0KeCFK6PcG+d2hSy8gbWsN+0unce+EtzEESAlmtRM0MN4T4mMV5U ISFxTPubmGboQRxHxyrWJOU43G3vqnv6c0B8Xj3DhjDoxfL/pyaC06RtIpIA2Cxvi3yx VFB+cyAae8gLiwy1N/punFQdIoYPU9/gELFdQ2vV/KYrptVx0bYTR0nXA8x2XDfFG+Ya 69Ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=H0BbxwVHNwnSrMPUgguGHD04mtg+HPKRQ7WxnV2HF38=; b=c1yxZcdF8cJdAc490KNV1JTsdc0Jdlv0lHrN2moA4/PuIQGtFENsU503a7pQewp8Tg xaX7LPeHWaK60i4rfO0XNpkwhbSvbVGW6gljkeQI2YEoMFyL/+i+bcvezu2NzRJuD1VM ChpK345sDnY6vJgGMUyj0gb4GDf7EAE1azfpNnN4c98nfjHPkpja2J8gGyesln8HGDxf tLqqx25cDASDpGT7h762RYQfRRhgasGdJ1V4Oi2rIgSBfUAA+i9LE7jxr3mfqy15iPqw QW/LB6Cau1/cg8xolJrGkV4gcMV7r7OczZnSu7nLiUs15cWurAF4Fk6bYoDvyWnK4ejW AToA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=biwKZPVP; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t8-20020a170902e84800b001870a181f24si3312830plg.222.2022.12.16.11.07.41; Fri, 16 Dec 2022 11:07:51 -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=biwKZPVP; 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 S231653AbiLPS1I (ORCPT + 68 others); Fri, 16 Dec 2022 13:27:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230089AbiLPS1H (ORCPT ); Fri, 16 Dec 2022 13:27:07 -0500 Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EFBC655F; Fri, 16 Dec 2022 10:27:06 -0800 (PST) Received: by mail-oi1-x236.google.com with SMTP id o66so1219975oia.6; Fri, 16 Dec 2022 10:27:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=H0BbxwVHNwnSrMPUgguGHD04mtg+HPKRQ7WxnV2HF38=; b=biwKZPVPRDWVDg8cbMLT+e9pIkZmNGSZ4+p/pPdCb3bqm64i3tTm4BzwAMVTyRExAc 5mv4THP+c0qpUpKDyoK7zoh9OozdEngsVQR6IBFdTS748NIymMdRgGu2KoAwXxHXygF1 IQtBNU+QN8hs3vUToyW1ALsxoMjebCNBNQfYVQVS8ztmzmfbZsG9xP+xOb+7L5APKqKj rpANIyh0bQr5T0VsTjjsvzk3j1RTBAmOSnalj0hCaI9xn7vznEPoOis8Q3chtrCaUnAe tBVjWz465ZxzGfmNETc8E9ZVwUAwyYKsiihiCfFG2yUBY5XBtsOncxHWQNthxh3Yycjr JENQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H0BbxwVHNwnSrMPUgguGHD04mtg+HPKRQ7WxnV2HF38=; b=j4IFDTApYRzQvJLJlbUlxa5xwkQmKRgJGvEk+fr2vFfsPaEppm/Ft7p+ErDxO/Rayk iuxZnl6fSBnviyh3JkxJM2Aw7BOmjg5EP7cf/nYWrp00xPX1ywu+bvPchISqJYXsAIeG QWrrdOrobrHh2FhBnv4Zlf2JmkGpLyna2Afbs408Hstl7fuJv2f3gegjJN88Uu5AJ2GJ wfIT4GbO+Wz7FKKHBqKT+1X1wRHjHO60NqsRvI2cPIiRV9oLikXYFP1Kj36x3OV9P5JP NndtRCmhABgIFA22J7L8GGABxn2mJh9JGLMMGodBcrnza6rbogudyPU2+j8RrOGH+zLz sU2g== X-Gm-Message-State: ANoB5plsPe6hrBTn39e3PRSBCM5Z9Q4kMo8d+QVbGHSUsu2ZDomjLjP3 0cNZsR4mU+iex04HMyGyBaoz95Dn8WYXYSRtEgI= X-Received: by 2002:a05:6808:108:b0:354:74a1:d0a with SMTP id b8-20020a056808010800b0035474a10d0amr733796oie.204.1671215225622; Fri, 16 Dec 2022 10:27:05 -0800 (PST) MIME-Version: 1.0 References: <20221216174927.1008629-1-wei.liu@kernel.org> In-Reply-To: <20221216174927.1008629-1-wei.liu@kernel.org> From: Wedson Almeida Filho Date: Fri, 16 Dec 2022 18:26:57 +0000 Message-ID: Subject: Re: [PATCH] rust: kernel: drop repetition in offset_of macro To: Wei Liu Cc: rust-for-linux@vger.kernel.org, Linux Kernel List , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 On Fri, 16 Dec 2022 at 17:49, Wei Liu wrote: > > It doesn't make sense to allow multiple fields to be specified in > offset_of. Why do you say it doesn't make sense? Here's what I had in mind: ``` struct Y { z: u32 } struct X { y: Y } offset_of!(X, y.z) ``` Which is something very plausible. > No functional change. > > Signed-off-by: Wei Liu > --- > Cc: Miguel Ojeda > Cc: Alex Gaynor > Cc: Wedson Almeida Filho > Cc: Boqun Feng > Cc: Gary Guo > Cc: Bj=C3=B6rn Roy Baron > --- > rust/kernel/lib.rs | 10 ++++++---- > 1 file changed, 6 insertions(+), 4 deletions(-) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 6a322effa60c..2f3601e4e27e 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -208,7 +208,7 @@ impl<'a> Drop for KParamGuard<'a> { > /// ``` > #[macro_export] > macro_rules! offset_of { > - ($type:ty, $($f:tt)*) =3D> {{ > + ($type:ty, $f:tt) =3D> {{ > let tmp =3D core::mem::MaybeUninit::<$type>::uninit(); > let outer =3D tmp.as_ptr(); > // To avoid warnings when nesting `unsafe` blocks. > @@ -216,12 +216,14 @@ macro_rules! offset_of { > // SAFETY: The pointer is valid and aligned, just not initialise= d; `addr_of` ensures that > // we don't actually read from `outer` (which would be UB) nor c= reate an intermediate > // reference. > - let inner =3D unsafe { core::ptr::addr_of!((*outer).$($f)*) } as= *const u8; > + let inner =3D unsafe { core::ptr::addr_of!((*outer).$f) } as *co= nst u8; > // To avoid warnings when nesting `unsafe` blocks. > #[allow(unused_unsafe)] > // SAFETY: The two pointers are within the same allocation block= . > - unsafe { inner.offset_from(outer as *const u8) } > - }} > + unsafe { > + inner.offset_from(outer as *const u8) > + } > + }}; > } > > /// Produces a pointer to an object from a pointer to one of its fields. > -- > 2.35.1 >