From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AEE152FD1B5; Thu, 14 Aug 2025 12:44:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755175478; cv=none; b=AQ/pzMhYlEoGmyC0uYorQmM6LP50iprD27P/oi+Kqt67jqVojdqznEPOc+elFdMwY9ydlzQPXeJrc0jAjEIT7VJhdRXiICpNzEJ4HPUrqim8VpgbT8ygE+sr4pUz3m9ugbGFte9otgCvha8HWhsyEo8XZQgp6Cjgd/1/ISyPi5A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755175478; c=relaxed/simple; bh=Vc/0ke8A/kCQqXn/Z5kx7ZmXdXZPaiE+Wn3603o+oIo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M9NJgC3xB70Je/I8No2MF2E9i82KrAqKHYfILXMCMdD5wvlLjuQiVrRQ0jzAU/0CQME/Syiwl3u6A7e8Ba11B3jO3qxbFRUPRBAy6ITN0DSKjhic1QrY3tvgQu1kN3HWWYhv4k+M+vnVmY1uY18wjq/ewvqDKrCTDHpP71a/d6s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sKBRmgeB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sKBRmgeB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCC6EC4CEF1; Thu, 14 Aug 2025 12:44:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755175478; bh=Vc/0ke8A/kCQqXn/Z5kx7ZmXdXZPaiE+Wn3603o+oIo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sKBRmgeBTdK7J/A5S0QgRfD3kO098jsr6Po8BAfXT561jbEpGTCumj/XYZmljQfWc FRNGx2eJL+qibuRTHBg4dJjBUclOn9JOjxdSFyhV5cL56O6uU+IEGr7Gn19WMukHoy 1EiofebNbux+/oCtbk9eNV4ZfeN0dScRVmlnTbHPwO336zRwTSkMXsyYASmNt9dVs1 njLKReexWtLWQa9gxmCNpDzN0TXlPXEuIcgi1kq9OlewoXYMe7rvsG7UStMYSFB3s4 LFwDQ+IeBJYuaRhlpD14b36S7QbyKiuXM0Jvidte7S5yJpKKxlbjpLpzumNMtKFbIO 96fctzVkAhjVw== From: Benno Lossin To: Greg KH , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Fiona Behrens , Aliet Exposito Garcia Cc: Benno Lossin , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/4] rust: transmute: add `cast_slice[_mut]` functions Date: Thu, 14 Aug 2025 14:44:13 +0200 Message-ID: <20250814124424.516191-2-lossin@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250814124424.516191-1-lossin@kernel.org> References: <20250814124424.516191-1-lossin@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Benno Lossin Add functions to make casting slices only one `unsafe` block. Signed-off-by: Benno Lossin --- rust/kernel/transmute.rs | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs index 1c7d43771a37..92410a93882f 100644 --- a/rust/kernel/transmute.rs +++ b/rust/kernel/transmute.rs @@ -2,6 +2,8 @@ //! Traits for transmuting types. +use core::slice; + /// Types for which any bit pattern is valid. /// /// Not all types are valid for all values. For example, a `bool` must be either zero or one, so @@ -69,3 +71,61 @@ macro_rules! impl_asbytes { {} [T], {} [T; N], } + +/// Casts the type of a slice to another. +/// +/// Also see [`cast_slice_mut`]. +/// +/// # Examples +/// +/// ```rust +/// # use kernel::transmute::cast_slice; +/// #[repr(transparent)] +/// #[derive(Debug)] +/// struct Container(T); +/// +/// let array = [0u32; 42]; +/// let slice = &array; +/// // SAFETY: `Container` transparently wraps a `u32`. +/// let container_slice = unsafe { cast_slice::>(slice) }; +/// pr_info!("{container_slice:?}"); +/// ``` +/// +/// # Safety +/// +/// - `T` and `U` must have the same layout. +pub unsafe fn cast_slice(slice: &[T]) -> &[U] { + // CAST: by the safety requirements, `T` and `U` have the same layout. + let ptr = slice.as_ptr().cast::(); + // SAFETY: `ptr` and `len` come from the same slice reference. + unsafe { slice::from_raw_parts(ptr, slice.len()) } +} + +/// Casts the type of a slice to another. +/// +/// Also see [`cast_slice`]. +/// +/// # Examples +/// +/// ```rust +/// # use kernel::transmute::cast_slice_mut; +/// #[repr(transparent)] +/// #[derive(Debug)] +/// struct Container(T); +/// +/// let mut array = [0u32; 42]; +/// let slice = &mut array; +/// // SAFETY: `Container` transparently wraps a `u32`. +/// let container_slice = unsafe { cast_slice_mut::>(slice) }; +/// pr_info!("{container_slice:?}"); +/// ``` +/// +/// # Safety +/// +/// - `T` and `U` must have the same layout. +pub unsafe fn cast_slice_mut(slice: &mut [T]) -> &mut [U] { + // CAST: by the safety requirements, `T` and `U` have the same layout. + let ptr = slice.as_mut_ptr().cast::(); + // SAFETY: `ptr` and `len` come from the same slice reference. + unsafe { slice::from_raw_parts_mut(ptr, slice.len()) } +} -- 2.50.1