Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp572573rwd; Wed, 31 May 2023 02:19:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7hGTrRqlcX6gRYOpHCR+XD6w+wYjwT7ujMZ2YdHn4TLiWKSBWYDf2Y4dVd92qbOBBx0k74 X-Received: by 2002:a05:6808:82:b0:398:111d:c441 with SMTP id s2-20020a056808008200b00398111dc441mr3129130oic.35.1685524779445; Wed, 31 May 2023 02:19:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685524779; cv=none; d=google.com; s=arc-20160816; b=AO5DVTiUI85xOhNVWB7H3aL27S2/oA+TG4NF9gWz6BOz+/Tp+zRrflhRoiFhsKJnBy CIogf3W1K/z5dj87VeLLiYGh9SY26LlCEy1VgKKWHV/GsnuDsCqRKEUtuPkRSHefML60 d2Vr/NZpBK7cmfrIjXxKRFNX37+liim1UH4B6FYsuOW7qtN1tlPJjKS+fpiiyu7HZ9r9 vLL7AHSETAOLrmYznQLpjHBDASye0xSivK3T7moGkK4MIFeeATcHb2eEk0c87FmVc1Re VEZuv/1A5cxvCItQeMammf3t6UA+JMYr29BP+3lOYgWdhcOEmuAt/pnkw1ocVEPhgWFc gvzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=PEW73TP0tUGYUfeakPneuxLod08yUyWlknx492xsU6I=; b=h9HwQW2DNq8J1Ck2zzW1aMMdtAyokvEuo9/sQhlIfH+TyD5SxXs7rmOd734p8oznmU SFdvJ3J0xYQLuidXeIaKQz1WSdKmOw/Zx6muRWHW/z8+FrDjUjeeiD1tZ+owEVnPPZcs OSC3aS0SKRVR3TK88Su3z9vwHK7h4eA/WTHNFum8/yA/tpGYUk/Kk9OwcjNuvXi41+5J F7gydHpmRQuz0q3kB00TzcOk04vxC+7uQVLyWzVvJM0T1CecyPXmhtn6frB8P816+jeN T4egdjPP2xeeHy6lFwfVKxW6TG7hb1cJPj3xoTyJQGSa+jPnmSHwlIamGvQkHhjfKRAR oklg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=gYPrGvgJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c23-20020a17090a8d1700b00250b2c89e20si631107pjo.102.2023.05.31.02.19.25; Wed, 31 May 2023 02:19:39 -0700 (PDT) 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=@google.com header.s=20221208 header.b=gYPrGvgJ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232404AbjEaJAk (ORCPT + 99 others); Wed, 31 May 2023 05:00:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234799AbjEaJAh (ORCPT ); Wed, 31 May 2023 05:00:37 -0400 Received: from mail-ej1-x649.google.com (mail-ej1-x649.google.com [IPv6:2a00:1450:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AAE4125 for ; Wed, 31 May 2023 02:00:36 -0700 (PDT) Received: by mail-ej1-x649.google.com with SMTP id a640c23a62f3a-94a34e35f57so466291566b.3 for ; Wed, 31 May 2023 02:00:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685523635; x=1688115635; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PEW73TP0tUGYUfeakPneuxLod08yUyWlknx492xsU6I=; b=gYPrGvgJZ60XP/b1C8CGziAC6W7bJnxHJ7ittHw6F1f1N/eMr+WR5Ljn5jcPp9fYT1 VTTggqdWq+M/qyWLAcHEy0y/c/8XGkXkT1+RMQGIf0+z8EZFVJMp7876YvnuYGa0BIHB HQO/5frgPLpxUsm/SqfeaqysjOI6I4dapjDMzJWh1btGSexT6s3PmQda0byDSMZyt3la X8bC9Rub6mAifjO/3+GlobSObhzbxuFPqV04JA/EZYaJNvQHia0s6B+xumKQjOQh7OCf f4nC4BGFTCTj008Xk1yJiyOhqcBihabUQ9/RbZUG10CACpxHDIfGb4chhIVn9oLCgmHx nQxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685523635; x=1688115635; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PEW73TP0tUGYUfeakPneuxLod08yUyWlknx492xsU6I=; b=eTlZu9Vo4pbipQhLHjmbJZt1bazvxUKHBLfVNyk0kR178uRr9UFtMQn6mXlfxip2tz OuPFutvrTdxRTM7B8Vy6ltaktAm3xDda5E9NT7hHPcnh8NRu/DaXs1t0kSSS/+3Z37eX H4NndUawXz6EaaLr3Ya9t9sBC+3BJXAeIINKVN8j3oL84gSbHM6ToWz6q8+8tmhKbqJM kbgdDkBEdEw4smg1DfldEKfD0oPOMn8joRjmLaN86dqOBkj3guxNMFzPaRa/X/RXB8PT N/xxbSeztf3AXqak8El7aizQ7wbl7Ur/YRoKd+tTUnieKJizY9gaIyY0daWt7HyhxOTi 3aXQ== X-Gm-Message-State: AC+VfDwVbeE8kMEqR3MMlSOOWYpfk49XkUP+f8yL6qrrljMUwKWZoJ+y xVT5jJqrfZdPTWxiyFZxCDQcUAu++Lch1s4= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a17:906:9bc4:b0:973:8412:2f78 with SMTP id de4-20020a1709069bc400b0097384122f78mr1812269ejc.2.1685523634903; Wed, 31 May 2023 02:00:34 -0700 (PDT) Date: Wed, 31 May 2023 09:00:32 +0000 In-Reply-To: <87leh69par.fsf@metaspace.dk> Mime-Version: 1.0 References: <87leh69par.fsf@metaspace.dk> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230531090032.3667467-1-aliceryhl@google.com> Subject: Re: [PATCH v1 5/7] rust: workqueue: add helper for defining work_struct fields From: Alice Ryhl To: nmi@metaspace.dk Cc: alex.gaynor@gmail.com, aliceryhl@google.com, benno.lossin@proton.me, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, gary@garyguo.net, jiangshanlai@gmail.com, linux-kernel@vger.kernel.org, ojeda@kernel.org, patches@lists.linux.dev, rust-for-linux@vger.kernel.org, tj@kernel.org, wedsonaf@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 Andreas Hindborg writes: > Alice Ryhl writes: >> +/// Used to safely implement the [`HasWork`] trait. >> +/// >> +/// # Examples >> +/// >> +/// ``` >> +/// use kernel::sync::Arc; >> +/// >> +/// struct MyStruct { >> +/// work_field: Work>, >> +/// } >> +/// >> +/// impl_has_work! { >> +/// impl HasWork> for MyStruct { self.work_field } >> +/// } >> +/// ``` >> +/// >> +/// [`HasWork`]: HasWork >> +#[macro_export] >> +macro_rules! impl_has_work { >> + ($(impl$(<$($implarg:ident),*>)? >> + HasWork<$work_type:ty> >> + for $self:ident $(<$($selfarg:ident),*>)? >> + { self.$field:ident } >> + )*) => {$( >> + // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right >> + // type. >> + unsafe impl$(<$($implarg),*>)? $crate::workqueue::HasWork<$work_type> for $self $(<$($selfarg),*>)? { >> + const OFFSET: usize = $crate::offset_of!(Self, $field) as usize; >> + >> + #[inline] >> + unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type> { >> + // SAFETY: The caller promises that the pointer is not dangling. >> + unsafe { >> + ::core::ptr::addr_of_mut!((*ptr).$field) >> + } >> + } > > What is the reason for overriding the default implementation of `raw_get_work()`? > > BR Andreas That's how the macro checks that the field actually has the type you claim it has. If you lie about the type, then `raw_get_work` will not compile. (See the safety comment on the impl block.) Alice