Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3432758rwb; Fri, 16 Dec 2022 15:04:20 -0800 (PST) X-Google-Smtp-Source: AA0mqf7+tQuaLf3wBtGgQneMO4kxrsJ+I0OXezFpnSK+yObRPG2RPvznvormvNAqMwfetWgL1lTa X-Received: by 2002:a17:906:a15a:b0:7ae:7c4e:195b with SMTP id bu26-20020a170906a15a00b007ae7c4e195bmr29075759ejb.22.1671231860614; Fri, 16 Dec 2022 15:04:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671231860; cv=none; d=google.com; s=arc-20160816; b=nFWdsFlCxutcS1ltTTROgWz0KBkDfQao2PC2TJjAB9KlOXa2wJROSyRpNpHrPFGwD6 xEVizPHzWlE9tNmvV7J822VFqRqOCGHJA+reANowdiERO0fXIydlPM3vPScqOfFW132R sajOxnT92sR+6o39K1HVqrX4SJEOC97VvYXEjCD7/0vzxfp3UzkIo8E2n+tWgAeRZGtG eNZxXOcd+zAHHw30wUAC9jHdVEe2ELkKgHnovS/Tio9XQ5xjRtOY7MkXOo4LEEiYFjws KTTITrmf6ZKRCWI8xYHcdmEleINxWtD6Sgo9u11GJ/CIeOHNGttNE92AWKcA2US+9qRM qzKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=Tz2yrlAl5b4n4vwlow3k9uKUHDFsLv1KcaPjbtDhLbA=; b=bv5UercCmYBRTLCds/iU1DmPS3XCSNwbIv1cv/pPZvjnfP+vsRnNEjhNc/49Hh9uHp 0ZISJ0rySKmj3DLIuGw6a7XnKvmrL3HEQLmEnoJa2+rJt7/LOJcW1LtSSmvPAxKl7CNU votCNRyZQMGTSjTxQEckuuFRDlytKT9D8LDQvQ1rQDjNuzxKyZA+qP5NKf1JetWbqD9C 78/IED+8+zChuq1Dtzxy2AF7eo67XDiJ0mcoIXgDY/l2P69sb9XTV399Vkgg5WfBqV4Z 6HORrq3tKuyBgX+iC8RkknZl5MuAt1Cm/N/ATgFNI/6ypgxDQplwycS8D/2CvUxsB/gf VYcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=m8agS7BE; 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 ml21-20020a170906cc1500b007c0889e324esi2795352ejb.366.2022.12.16.15.04.04; Fri, 16 Dec 2022 15:04:20 -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=m8agS7BE; 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 S229844AbiLPWP3 (ORCPT + 68 others); Fri, 16 Dec 2022 17:15:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229453AbiLPWP1 (ORCPT ); Fri, 16 Dec 2022 17:15:27 -0500 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 326BC1F2D4; Fri, 16 Dec 2022 14:15:26 -0800 (PST) Received: by mail-qt1-x834.google.com with SMTP id h26so27827qtu.2; Fri, 16 Dec 2022 14:15:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=Tz2yrlAl5b4n4vwlow3k9uKUHDFsLv1KcaPjbtDhLbA=; b=m8agS7BEjEUtwpDVmjnLFYUPMyW1gsL95j+okJxy+BVBsObjDiYYPVT9Eg5yvSbPoy yFtt6hp+kJ3z1Y2pWsyS34890m+HzgBCBS9ItrWo53deeVWCSLdPepYvEoJWzxxm5ME2 erD2v7+8TSbD1f1zJQT2Y1J8yW1VCspDF0hKRhiCN/euWywkXaeCtp284vfb8GINuJFB CQ+5UvbzSoghBYW06ZYGBh4OZ903Erc7OVknUnt/o0B4oyP/noIAkOkgWtN067Mv28WZ wLUXp1TVsOH8HlutbPBirh4Cytfr2eOz/gCnBldsBHlhu4WMyWql/2Lwze38gASMh+N8 mpPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Tz2yrlAl5b4n4vwlow3k9uKUHDFsLv1KcaPjbtDhLbA=; b=WzvOPyt0y7hOfim2Z1KkgpiPqjZXjrXwbwLmRYuV+D2MkC/u0fKfwW8gzTdocM5lrs FnQnGBRN7UtK2xtIV1jyk2T0glAaPoP0ON+srGPZKto2/qOyChAn/d8O5ONI/PeEej+2 iZRrwANaJ23UY5frHkfhKNhskvp3Fz0+90ShY6I14AbFv/dYj4YMBOyCDbhzcgmIIycd +PvOmvdIyZyEw3X0dAYhVYFX7yC/ou+JnDK4J+Hpn6Zot1+ZSGrIY2OHuSQCnBiUQVa5 5Ih+noGnqVff6nX9LCgaeKyrA8bA+oSnJ4hNj2u2Wsqhv++Zx62pdPFKh2QgweZiN767 LxoA== X-Gm-Message-State: ANoB5pkYNu/vfx8sc5wUwvWnWIezxXB0DJjCVKr2WESXHgigi3/+aFED ooqh8wsvdvUXKn9rwdJjky0= X-Received: by 2002:ac8:481a:0:b0:3a8:2ca5:8f9b with SMTP id g26-20020ac8481a000000b003a82ca58f9bmr17114846qtq.16.1671228925239; Fri, 16 Dec 2022 14:15:25 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id p16-20020a05620a057000b006fee9a70343sm2251583qkp.14.2022.12.16.14.15.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 14:15:24 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id 33FAA27C005B; Fri, 16 Dec 2022 17:15:24 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Fri, 16 Dec 2022 17:15:24 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeejgdduheeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggugfgjsehtkeertddttddunecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnheptdegheelveffudejffegvdelgffhhfelkeeiieefgeevteejvdegveeu ffeihefhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 16 Dec 2022 17:15:23 -0500 (EST) Date: Fri, 16 Dec 2022 14:15:16 -0800 From: Boqun Feng To: Wedson Almeida Filho Cc: Wei Liu , rust-for-linux@vger.kernel.org, Linux Kernel List , Miguel Ojeda , Alex Gaynor , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron Subject: Re: [PATCH] rust: kernel: drop repetition in offset_of macro Message-ID: References: <20221216174927.1008629-1-wei.liu@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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, Dec 16, 2022 at 06:26:57PM +0000, Wedson Almeida Filho wrote: > 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) For me, it's not very obvious that "y.z" is multiples of token trees rather a single token tree ;-) Maybe some examples of the match pattern of macros can help people catch up faster? Like "y.z" => tt [y], tt [.], tt [z] I will defer to Gary or Bjorn for a better quick guide of Rust macros ;-) Regards, Boqun > ``` > > 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?rn 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)*) => {{ > > + ($type:ty, $f:tt) => {{ > > let tmp = core::mem::MaybeUninit::<$type>::uninit(); > > let outer = 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 initialised; `addr_of` ensures that > > // we don't actually read from `outer` (which would be UB) nor create an intermediate > > // reference. > > - let inner = unsafe { core::ptr::addr_of!((*outer).$($f)*) } as *const u8; > > + let inner = unsafe { core::ptr::addr_of!((*outer).$f) } as *const 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 > >