From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from CWXP265CU009.outbound.protection.outlook.com (mail-ukwestazon11021101.outbound.protection.outlook.com [52.101.100.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C7103909A2; Tue, 25 Aug 2026 12:01:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.100.101 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787659318; cv=fail; b=pqsznSuQDTsl/VfyWQzy4fEEjhR/bJqIHgS7B6Rg1R/3Kf1NWILZlIesur52kJIcvX6hGIPtk5CzgTyRFnfPBe9z6z52EVr3gICwrZfuAd+9eiv+pT9TeITM7v+LG63mVh2xWE2aKmeMGKvsXcqLYr10Sznbczz1mMJj+VVOgZk= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787659318; c=relaxed/simple; bh=48hE24saoXz9VZCRi6T1YLiiAtJ8Iehc/g3V/wYcz/g=; h=Content-Type:Date:Message-Id:Cc:Subject:From:To:References: In-Reply-To:MIME-Version; b=lHpcf2vStqsx4UeYdloKj6eFSYkwMR54zcZQ/xWeWAfxbIqXBFXbVQRQqOU31rYBC+SHAsEMZCv/QMl59uTcTC5MqYiu1QmB/2Tfu8CJ3W/iUqYGhr1ljJjYvplYdzzSDOqTgwPm1aAlA4J4uBivdtVQwrYLJTu56ix33vz/cVU= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=garyguo.net; spf=pass smtp.mailfrom=garyguo.net; dkim=pass (1024-bit key) header.d=garyguo.net header.i=@garyguo.net header.b=adgvochW; arc=fail smtp.client-ip=52.101.100.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=garyguo.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=garyguo.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=garyguo.net header.i=@garyguo.net header.b="adgvochW" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nI4oup3NcW3Y1oRZyrGVRSXqjzsbWgY49G4fRwQy1GgN81cqoNBUhMSrqbTQzpN0eoE3lKnGb+6nhXCVxqo2UxSDieTKIeGiz4w22nvDzjfVo/rOZkAl1eI+koWkVPxK4f9VEUxSU9bVT69CK8VCivh+UWaT8E/0aQH5V4BaHrj5qH96uGBNrwhsL5jGhfU+aLKxvnQXOsNdyXRu4IPmIDh5qoVeB18hBmoGwog+RXbO6GvxhZrOQx4A8WhUxifJhWFEYQjO83sIlMr+Vu+k2CMGZDy/x2Hvg2v06JBOLUDd1lAE+Arym3MhNWhQI1xYqEMcUOvNKvI4f4aE/IoNqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=C4uuKvNorT2lulgI/7lG3j7xTRD9O6cOezaFWsSQVYE=; b=gf17LKbd9bx8vW7VDY5usGCQLAELBikeKhsExJ+a4YsfJriEELCpqbmXVk+rhj2ldpSGzwsS3xH7xNT+jHE5Qj/0oAsrwsy13ecfyZPecoj6OWdpgWua2lBzqifp43dZVgbkrb480obkgt/YJQjcstLGYlFnPpRx8acsd2jHDV/TJIJX3tiQXzH76Mju09qWX3TKKnr82wuNs4ESTWy9isk04Eelv5SkirZBpP11edDpQIiyJnYiHZJnbYEblrSfZomzF3B7PtZl6m+6EGVshl+vwOrhtkEq0VRlsipSr1LMFs4ZpHG3qGFQHfch6zsv2IjR42toro0Jw1ij5zI/iA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=C4uuKvNorT2lulgI/7lG3j7xTRD9O6cOezaFWsSQVYE=; b=adgvochWDdedIWdbSQ5zcmCe+mnRw3SApFJaflNOQSjV34Nu/FDuH4v650sZ/4qG9O/2Vdc5K7Ec/2bDZqYVt9u84oH4NWvHVNgbNyGGMO36tRaACpri4VbafhnGZ3Jsx8cf/WZ9lhBJ8wlZQ1hmYXUK+LqAytZis9m44NqW6wM= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:4ab::19) by LO7P265MB8764.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:4ec::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.360.6; Tue, 25 Aug 2026 12:01:43 +0000 Received: from LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM ([fe80::f60b:1537:68d7:4fc1]) by LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM ([fe80::f60b:1537:68d7:4fc1%4]) with mapi id 15.21.0360.005; Tue, 25 Aug 2026 12:01:43 +0000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 25 Aug 2026 13:01:42 +0100 Message-Id: Cc: "Yury Norov" , "Miguel Ojeda" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Daniel Almeida" , "Tamir Duberstein" , =?utf-8?q?Onur_=C3=96zkan?= , "John Hubbard" , "Alistair Popple" , "Timur Tabi" , "Eliot Courtney" , "Zhi Wang" , , , Subject: Re: [PATCH 1/2] rust: num: casts: replace const type narrowing methods with a macro From: "Gary Guo" To: "Miguel Ojeda" , "Alexandre Courbot" X-Mailer: aerc 0.22.0 References: <20260825-const_as-v1-0-1ce712225fe2@nvidia.com> <20260825-const_as-v1-1-1ce712225fe2@nvidia.com> In-Reply-To: X-ClientProxiedBy: LO2P265CA0482.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::7) To LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:4ab::19) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LOAP265MB8560:EE_|LO7P265MB8764:EE_ X-MS-Office365-Filtering-Correlation-Id: f17903e0-1212-4172-63a7-08df02a0a14c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|7416014|376014|23010399003|1800799024|366016|6133799003|10067099003|4143699003|56012099006|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: BOqPow6YOyKidWqxwqaFkuHugYtlqA+mEHOQFWuoYl3NI9zI2Qb32mf0yo9z7c7UAKH5wIOlXJYXxJ+eI2rW142E5I+NvBZtLonayYetHCPUaNzpAMPwGR/OrM9gwFbTKQpxOWxGDTd8mr9aGVK2GNYRMpmDB4vB12F4FS/n8nW7K4YVh/aeOHSpYmE8g+gwxD2qck6YtwbQYn+f0kCDxLbv0pHBOzcHjykCutfHimZphfxFdp5dQUo16ox0bX8z2fvWZnm0+po3UhkYX8OW3jwaQ4ruOqjNMUV2+aaS0W6fGpxfHOQ1hJ8goJPa9IPnGimTgJ28+JpxyyEcmn+6VOfjyIjYoD89ajYwDnxDP3a+lVhS4Oc21PPfGrQm6oKzOlD3flD8Aeh2GTriS0N/RtNAv1SQYlovxgD8syR7zRFdgTMfu36OhgpsHZzTdWjpcVVj4QK6yA7XbTQSG4Dl+TznfBjsJ63HnbnmNhhEARDoUvIq7ec8XUkIOtLnplwqFGUNsxDhb7g2xRRbDQ7KijOkrn3Sx1vM2G2OcVPfq4n4YtTD+SRlsjFQUt3kmq7Hyn1TF4tZGLgPDBQ+zWiSOR5kCLBRwAY7D3GeYoLqiQfUwFJR3CiJhupbyhmrMJO6bjJWlUN4yruPfKUFm3Bj3Bs7CSh2jQr3GTUIfhvfCzs= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(10070799003)(7416014)(376014)(23010399003)(1800799024)(366016)(6133799003)(10067099003)(4143699003)(56012099006)(22082099003)(18002099003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?T0ZqcTdCQlNJaXZmTC9FV01uZFl0RFFKSEZvNjZLV3lCaG15a2NFcDJPc0tF?= =?utf-8?B?V0ZsZmV6YmJmUnlLdXM4STZvUVZhL2F2RnNqTjNDbkJjNDNpTTZyd0JRNVhE?= =?utf-8?B?N25NbHhvZmdCTmlBMWNIbm5IYTdZVnFFVFFheTEvK3pHSGF1aVZMVmlXUm12?= =?utf-8?B?NUt6TnZwOGVPVGFBRHZ3T25wc01WeGtQd3YvdXQ4WkhFS2hYbDMyQ2tiUExX?= =?utf-8?B?VmtNWEdzc0M1dlNlbEZFR2JoU2hlQTR2Yk1TZ3ZDK1hFQzFIc2Jhd3BXN00z?= =?utf-8?B?c25GeHZDb2RkZzlBREdnUEE4Y0xPaVRVa0xXK1lDVXpqN0N4eW9vT0Jtam5y?= =?utf-8?B?N3d6TFZlYVdIZlpsOWZ2TTlzeDRVc1ltMm5ySXM1YUZzbjBNYm8wK3FUVDVa?= =?utf-8?B?d1NrMG1qam5r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Ud25abnY4RjdiNXd1ekJaN21G?= =?utf-8?B?Ti9GOTBzNVB3Q1RTL0FTOXVmT2NrVSthbFhUbE12U01IS3padW13RzZXZXph?= =?utf-8?B?UTZMYm9JR0RkRVgyczlPRDgvWndmY3FOYThzN2E2L2JyS0dlN3Zidz09?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: f17903e0-1212-4172-63a7-08df02a0a14c X-MS-Exchange-CrossTenant-AuthSource: LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Aug 2026 12:01:43.5506 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zPs/iPZxvzZO/h/bYXAcCoT3gCY+Il4lH10G5uSqWIs8kS1LMezVASS1QBcv/FDwUs3123JPuYd+85hVTA7pBg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LO7P265MB8764 On Tue Aug 25, 2026 at 9:25 AM BST, Miguel Ojeda wrote: > On Tue, Aug 25, 2026 at 4:45=E2=80=AFAM Alexandre Courbot wrote: >> >> const DMA_LEN: u32 =3D casts::usize_into_u32::<{ MEM_BLOCK_ALIGNMENT= }>(); >> >> into >> >> const DMA_LEN: u32 =3D casts::const_as!(MEM_BLOCK_ALIGNMENT =3D> u32= ); > > Hmm... I have been following the discussion and listening to both > sides of the argument. > > The macro interface looks obvious enough, and we could consider adding > it to the prelude. > > Having said that, macros have a cost too when they introduce new > "syntax", so since the beginning we have tried to minimize their use > to where we feel is worth it. > > The former line above is not perfect by any means, but it is > nevertheless syntax that one needs to already know. Personally > speaking, I don't care if I have to write the former or the latter, to > be honest, so I am OK with both ways. But I worked with C++ TMP in the > past, so my eyes may be desensitized. :) What I have issue with is to have things that do not belong to the type sys= tem in const generics. Expressive power is really limited in it, so any expressions involving generic parameter, for example, won't be usable insid= e the turbofish. It also doesn't work for custom types, so the approach does not generalize. The type/value distinction is one of the biggest change I made to `const {}= ` implementation, many features of `const {}` is possible (e.g. reference to generic parameter freely) precisely because values don't flow to the type system. Yes const_generic_exprs is being worked on and it'll blur the line between = type and values. But we're not there yet, and I think it still worth having a cl= ear distinction and we don't put things that don't need to be in the type syste= m there. > > Apart from readability concerns, we are saving here a few characters; > getting possibly different codegen (forced textual inline), and maybe > having better or worse compiler-side time/memory/disk numbers. Is that > about it? It would be good to measure any actual difference. The macros are much better than functions in my opinion, because it compose= well with the type system. The all "foo_as_bar" methods also compose poorly with= type alias, where this macro doesn't have that issue. Also, values no longer end= up in the type system to begin with. > > What I wouldn't want is a raw `as`, because the point of the saga we > started a long time ago is to introduce better tools that allow us to > get rid of the almighty `as` into weaker (i.e. safer) options, even if > some uses of `as` may be "obviously right". I think that is rather a linting issue, not something that warrants extra c= ode in kernel. We have been requesting some extra clippy features and I think t= hat is the correct way to go, not add a ton of methods and macros. Yes, it wasn= 't moving on clippy end, but I could add a feature to klint instead? Do you think we still need all these extra function and macros if we can get clippy (or klint) to enforce CAST comments? I can imagine the following rules that would practically solve all the foot= gun of `as` numerical casts without having to use awkward syntax: * widening casts are allowed * narrowing casts is disallowed unless CAST comment exists, except where it= s value is constant and truncation does not happen. Best, Gary