Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp662746rwd; Wed, 31 May 2023 03:47:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6WNFJjJIe0mJj6dfwQY1UQRbYLsAAfI5uZvXIVzPZkaLRSp1ynsfT0mjxNqE1WUWKjNjQt X-Received: by 2002:a17:90b:30c2:b0:256:728b:9672 with SMTP id hi2-20020a17090b30c200b00256728b9672mr3756809pjb.22.1685530046420; Wed, 31 May 2023 03:47:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685530046; cv=none; d=google.com; s=arc-20160816; b=04VgPaz+JWI/Gt2TBnairnEKPJpBxVPk3eQAIcC0mWYCkYq1Ph4L30fDYGqjvDA9fV cAYbny1yZJEnE+9IndnoXCDu42yPI4UO6mLTOp8VxGJnHpZYt54DxS/sCSgMxpfsIrbX ynRCeHTicW/7Hf+sCskkrReHOYjFkV79gFk9VrXqj1zNLh03z1hrI5zFecrkr2GQGqYa jgIvG+O1WRwnFHCYdm1bCpFYNBKcjQYddLQt2KPnPxRzNT/Gd4Eshc9GeE8tuSmcOQko 2S/9G48JwRAaVXevBM2PIGi+ODKsVU1+EZmD0fhQSx3HDakIgSHSLYubO99pnISFyHo1 0d2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:in-reply-to:date:subject:cc:to:from:user-agent :references:dkim-signature; bh=D9wK23t2sGNzjDkReOB3GT1QVBAdm3GmQJMkozq40P0=; b=dHNWhe4UYNR9X/ZYxlnS4kR9jDoRqPpNiG6dNlER4DUbY4RfTPqHRTN9+00ZUtFRcK IFVKsKg4GJYj5ALjdvAF3moMgLu4ghi+H1QdjOPkShS1tmbW4XokeG7M6Sg3RTXfZblJ RVnovaoE/qCR4tA1/XvjDqUAtVD5EfdcGXGvZh2bMrGz01IGYp0t1/Y+7SLk7S6RuEjo y6mjhx4+1fT41ao2Q/aBL6eObIfdZbmfxOhRFopegGx2o1AJPqVZx0OdRp+YjPiUfGLZ BPrc1FML9YLukOYtk8e8dC95O6QUYU7XfJ9Abf/1/1kd/cSkpnqpvL6HfH+TXH8WVE8F ZibA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=AIu+b8G0; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b14-20020a17090a6e0e00b002503ba697e9si746652pjk.131.2023.05.31.03.47.14; Wed, 31 May 2023 03:47:26 -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=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=AIu+b8G0; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230470AbjEaKlB (ORCPT + 99 others); Wed, 31 May 2023 06:41:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231397AbjEaKk7 (ORCPT ); Wed, 31 May 2023 06:40:59 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39164121 for ; Wed, 31 May 2023 03:40:57 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-5147dce372eso8178685a12.0 for ; Wed, 31 May 2023 03:40:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1685529655; x=1688121655; h=content-transfer-encoding:mime-version:message-id:in-reply-to:date :subject:cc:to:from:user-agent:references:from:to:cc:subject:date :message-id:reply-to; bh=D9wK23t2sGNzjDkReOB3GT1QVBAdm3GmQJMkozq40P0=; b=AIu+b8G09ha8tBpJpcXJQCPeq0ed4+ggJKcW4dwZrO08fXZdQ2tw23V6SU1enf/pLe xz7/xU4WnxBkRiTFOQY9UCLBHxkOl1HE1FQp14w8Cb7OqtWtkqHxU47p2MWZTfI2rljF +mWsRulFmiOLUXs3IAEPomaoUPhs2XbihXDKeq1AlPIa3OBboQjHMM190lvIxiLU+Mur 8q0igSTFZcpB+JYr1Jbrn+mKnOrrESw+GQZI21hx5lIF3gAFAk3QitJ81ceVFc+szrCF iR6Sqn5FZHQDvrZJEEMh4BDAEHMTi3Sen5rp4moPXJwgg0MXry5YG4eBhHEnI2SFicW7 yCqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685529655; x=1688121655; h=content-transfer-encoding:mime-version:message-id:in-reply-to:date :subject:cc:to:from:user-agent:references:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=D9wK23t2sGNzjDkReOB3GT1QVBAdm3GmQJMkozq40P0=; b=ZbeWcKsGfhpPK7MmB+TfaktxE2eDrVGhTQjymzck2942SmKGE/RMq7xrW7Z9bbKe5i TCiRxPD7YShV8u8Tib6xz2Nr4sGX+5tXxu4v2IMuci6pg0DgiN6lomUEUH6ENVJ416WD FZIKptGJDtXeATkzjA+MLg2x6GKZNI5zk21aJ3P3zcZmsvUG6nDjGcMCMg/xwXgUmEEq AdKenf7enKJoSvVBpEZi5BJ+rCwEonxEtZ4W6/L23x9JT1J7nTmPKiYEdKRoTImjuE0J vGa/ZiOTQHbB3kloC3y1Aqv8Pae7ZfclX8iB2mHvzdveWl7mjt7vlIVqwDbE7n4pWgmN Kjfg== X-Gm-Message-State: AC+VfDzqjpvw0bLME+24KlO48wcBUhlKJa3K09VdkyldDCble8EG7qo2 FNN70z7IJpSeiD9jvKnkyavssg== X-Received: by 2002:a17:907:ea1:b0:957:48c8:b081 with SMTP id ho33-20020a1709070ea100b0095748c8b081mr4688011ejc.24.1685529655648; Wed, 31 May 2023 03:40:55 -0700 (PDT) Received: from localhost ([79.142.230.34]) by smtp.gmail.com with ESMTPSA id lh27-20020a170906f8db00b0096f03770be2sm8697953ejb.52.2023.05.31.03.40.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 03:40:55 -0700 (PDT) References: <87leh69par.fsf@metaspace.dk> <20230531090032.3667467-1-aliceryhl@google.com> User-agent: mu4e 1.10.3; emacs 28.2.50 From: Andreas Hindborg To: Alice Ryhl Cc: alex.gaynor@gmail.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 Subject: Re: [PATCH v1 5/7] rust: workqueue: add helper for defining work_struct fields Date: Wed, 31 May 2023 12:18:47 +0200 In-reply-to: <20230531090032.3667467-1-aliceryhl@google.com> Message-ID: <87zg5k3hs9.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Alice Ryhl writes: > 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 } >>> + )*) =3D> {$( >>> + // SAFETY: The implementation of `raw_get_work` only compiles = if the field has the right >>> + // type. >>> + unsafe impl$(<$($implarg),*>)? $crate::workqueue::HasWork<$wor= k_type> for $self $(<$($selfarg),*>)? { >>> + const OFFSET: usize =3D $crate::offset_of!(Self, $field) a= s usize; >>> + >>> + #[inline] >>> + unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::wor= kqueue::Work<$work_type> { >>> + // SAFETY: The caller promises that the pointer is not= dangling. >>> + unsafe { >>> + ::core::ptr::addr_of_mut!((*ptr).$field) >>> + } >>> + } >>=20 >> What is the reason for overriding the default implementation of `raw_get= _work()`? >>=20 >> 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.) Got it =F0=9F=91=8D I was thinking we could do the type check without redefining the method, but that blows up complexity wise fast, since we need a trait to do it to support `Self` in `$work_type`. It strikes me as a bit of a hack to overwrite an otherwise fine implementation, but I guess it is the least complex way. Also I am a bit annoyed that we need to state the `$work_type` type at all, since it is available in `work_field`. But I can see no way around that. BR Andreas