From nobody Sun Feb 8 10:22:03 2026 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2713121D591; Mon, 10 Mar 2025 07:30:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741591860; cv=none; b=eHqP7mafwMMIaJ/p/A68EaaWoliZCKoGLJi0u0WsuA+7NHVOGvz1jedDLGO0eJWAUfR+ziUNVOP/SRQqCEAOqln4mWVCAvjdHSOolAY0Cetuo/0wU1hdWHT6jKQIaFOsFVjlmYwQ3UPygeAdnPA+FSZq1ATy9SrUTz4T5W7mQBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741591860; c=relaxed/simple; bh=ZWyiDX/vDGiAqA0tRjbykGLrDq3j4KxRc+K/r5na4aA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XVe2VMY+C3A+R9gjQYzc419Wx81mGK8BhGgc0FMv/HcS2OVgDYSvapTH7hgKN0pOdmNzMhBwTAPTIrS77nUQaeMJAzNDciCequVJGvbme5m7IaC3i5cYXWBIbfW3klW2Oqlqo5OaV/gK5TOp0lHGCgM5ABn/7HVoIY6HWSTA+7E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hKvvApxm; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hKvvApxm" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2255003f4c6so19586525ad.0; Mon, 10 Mar 2025 00:30:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741591858; x=1742196658; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gbrw9fKYJUWReB0Z6L2vCwEnh6k+VDvJH5rqaU18azM=; b=hKvvApxm+QhoLF1gNXYPiGPy9ZUhugr68/vybN8M8SxKvur7RLZ5lBsMmoCO7oAZUV cI+bfD6qup+8GXuSdlLVkAqtu2j0koSj9gGbEJ2fvzyU4P9LNKMlRUEbImRIOxRwkW/X LFNiA018oc7oVv0asvTJEjkUj9UtXF+3FZEMqQMm1Izxqkjz7SSKwjGdR/augaSQ/IrC DvxYBQIon/37maaFpx7RU2dwMQJRldXm6/4hfa/YWKBeYYHbtF6K36/X2RjdW6mCW33D Zu64A/ePbC/KcPbrP03xoHV0YZ7bPyCKTrz13oCOaV04fLR2rGraF4v6QB5SMXNb1Z9h 31jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741591858; x=1742196658; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gbrw9fKYJUWReB0Z6L2vCwEnh6k+VDvJH5rqaU18azM=; b=DnO5dumLVF2chxI3e+rCyTxLJIcYlYlQXTQ+knX6FupSGiVy0UCUzYF690zbdMUKGw F7LnUmKncT7/F+w7g+166+VBkjsjO799tPO+sUpYaVhvRWDeFAzDkgmG5vOTTyKzxSdB 1dy6tE5Jdb//kdJHFLXL7tNFnQlVjICb/5wJbbEdaguBjjdku6cIABKdNAD9BweXuE7Y jeZME3qtRWDTQSk4Oy+3obE52ruiUGxKQTQ7oS7knfbbDAKJwLIz68PFVnDmZh53TTNV t6u/maajCLn2Ne6tPOaXwgJPJbEqE4zie16NWRlSZJn7bfmQkiA8Q04BSzIWWuOpnYIW BGTQ== X-Forwarded-Encrypted: i=1; AJvYcCV/KQrWzVEJGFV+9BFQO0BtAuX2erA3YNhwOswNovf6CijA12NWAAPNIRVlONPDfkVfmRZHI/CSlWyfDdk=@vger.kernel.org, AJvYcCVshdT1DNrxiqNUdwAmTxXw9fl8qxHQTqZQWM0zwJGqh+9LrzcdO0BEymQODcAErj7Z1XqEi6dSOAYF0v/jmHA=@vger.kernel.org X-Gm-Message-State: AOJu0Ywf8ytK2mH35ZUmlsEK4FZLHwayFrvNKEQa9HjhcRiGrJPjjnv/ d8FDw7LtmV10EJvTfsyM8aFEkmikton7cQ4rUq2bPOwPgurVZhRt X-Gm-Gg: ASbGncsO7fZrEq+Qw6PWwEQRzJdKbf4uXlvGq6cTeR/vSKi21OlihL0kK7XxqjI2S5c Kql7QgFnEgRsv0S9xsjfh0Ro0GPxRpa02PcZnH9EliAzlZYNIglpItoWy2BKpmd8bafkLQ8e29b /hb/Fsh7i/sK3SVFk1SQueKbOCGk22zI3XIoSP/QaKFz7mifcxRqqQcV40/GT/Yc07Rp9CAwt2M ecrIQ3Gj1LTU1sUGBAxyX5SzmHgO7+Q4f2FjWU/9NW3PV0KPTHThAds6WWow+Sok1e/PLr2aVVL XychNDzg1pTfB2fGokRg4+651h+QcysjAmvySV1uxa8+JOomWIeobWLCggwRhkpTPkZVKlLuDw= = X-Google-Smtp-Source: AGHT+IHsKs1IEPx0zVJOtYDvg2NEVgoS3BbGZqnYfDtmOAWe1ErF1lt0JxUII7ucqC81s+4W77tleg== X-Received: by 2002:a17:902:dacd:b0:224:2717:798d with SMTP id d9443c01a7336-22428bf725dmr181608585ad.50.1741591858392; Mon, 10 Mar 2025 00:30:58 -0700 (PDT) Received: from vaxr-ASUSPRO-D840MB-M840MB.. ([2001:288:7001:2703:7ad0:37c7:5275:4b0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410a7f773sm70840715ad.117.2025.03.10.00.30.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 00:30:57 -0700 (PDT) From: I Hsin Cheng To: ojeda@kernel.org Cc: alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linux.dev, jserv@ccns.ncku.edu.tw, I Hsin Cheng Subject: [RFC PATCH 1/2] rust: list: Implement normal initializer for ListLinks Date: Mon, 10 Mar 2025 15:30:39 +0800 Message-ID: <20250310073040.423383-2-richard120310@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250310073040.423383-1-richard120310@gmail.com> References: <20250310073040.423383-1-richard120310@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently ListLinks only supports to create an initializer through "new()", which will need further initialization because the return type of "new()" is "impl Pininit". Not even "ListLinksSlefPtr" use the method to create a new instance of "ListLinks". Implement a normal method to create a new instance of type "ListLinks". This may be redundant as long as there exist a convenient and proper way to deal with "ListLinks::new()". For now it's introduce for the simplicity of examples in the following patches. Signed-off-by: I Hsin Cheng --- rust/kernel/list.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index fb93330f4af4..57d75ca16434 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -158,6 +158,16 @@ unsafe impl Send for ListLinks {} unsafe impl Sync for ListLinks {} =20 impl ListLinks { + /// Create a new instance of this type. + pub fn new_link() -> Self { + ListLinks { + inner: Opaque::new(ListLinksFields { + prev: ptr::null_mut(), + next: ptr::null_mut(), + }), + } + } + /// Creates a new initializer for this type. pub fn new() -> impl PinInit { // INVARIANT: Pin-init initializers can't be used on an existing `= Arc`, so this value will --=20 2.43.0 From nobody Sun Feb 8 10:22:03 2026 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CE71221735; Mon, 10 Mar 2025 07:31:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741591864; cv=none; b=rqADE/alEt2+sIH4+g8K62vTOMmoEnw+VVRvQw11sPhZwSLvh2XJ6vc0EIouwzLtNdUuVA/Re3r1tBDJJ5JYNhE+y+YKFxnpsojcgyRt8k2cDXrpeklpN1aLyFiRbsjDD0SzsX3PGiRiHBCI7+4vGnBQ0Qc9c9E4N7cjrNMsGEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741591864; c=relaxed/simple; bh=Zu63HHkaNmeHO+nk8MSfP7JWXnAppM72Ccgnmk4GrTs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=domwyWDjd2w+ED9s9/U0dxw8AWyWEv13fSGgTmCwahUjjfEcjIcgnkMYZW9bdb3uhuJ9k9UxZkXrKgH9l6cdTI9We208F7F7RuQiOa3NvXDxOQ5uAemYiBLxULA4yLIaUdctIPPczPk7s+1l29x5w8ZopRdzERIoC5J9TsI7rQs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=e7JHCoib; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="e7JHCoib" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-223f4c06e9fso63584375ad.1; Mon, 10 Mar 2025 00:31:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741591863; x=1742196663; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pKmv+q9cPJk5vyRVGZkQzjZQAXXzEnVw5/P1XfmlNVQ=; b=e7JHCoibHkvrKl4eTavvbUFHULiPeZ3gGxStlKfg+NTIXyfoUhvaMnjJ33JMFaFsm7 qqzA+VNHB9MWtLsoTH6gJW6yZjMSSciv4H7lfc1eNCPINPjuOKPLGR30TAAImaZQoE+J BHU9VbJOniwB7WcnjtIFSJTD1WgSzOGVKEUe8ftqEnJji3uGhlL0dCRrqg7YecfGWZIr g6iYXaR0MoYrBmqWgGguk4L8MIfBHPz1uHQReL2pnP5EPwQKqr05+iEhCoy8cRAuIsFu fzdvHrhdsIzbqln1psPeH7ei0TeBJ+3dwyoWOknxtFJuNH82gUVIeLxZZylDsVYuTifb XbZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741591863; x=1742196663; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pKmv+q9cPJk5vyRVGZkQzjZQAXXzEnVw5/P1XfmlNVQ=; b=iB1bfYu++ncmw6LPuYLTKuN/KAn1Z5dSIYpuVoxyYbwsvUyUdWQFtJ9ZFRpC0PUog7 T/q+Jz8z99VzG7LuU6DNXm2yfgoNXiq7qdBghWNzpaxdvvTy7qr5ktnosyF6mgOun68+ /wI2XZsmoWKp058odP8uOxrYVfLwphaksPwuEfL3908QyhO+67BPp0Kd+Iv147xNIL1e 4znRdoB7p+HbjDFrY+op7xSnmyIGqirOD0QR1QzUcNHBz8rq7oWSvzME0IVRDNerhVuQ Y/GMapRcwzP89bCWjkSZ2HX206TD6LVUv9L5aRM4Zl0gwci9ADs4pg6m5nZ1zCl26z6+ TQRg== X-Forwarded-Encrypted: i=1; AJvYcCU/uocl+kR8CfN5TBm7nUC618bn0hs+qI30OBSrJL84YsVS2mIxNitfewjIaGwChj8Epv6RW1yrFuHv8XQ=@vger.kernel.org, AJvYcCUkIDZQSgIc3luwHq4PPd4AVcKXj04LkfQTc2jlTwqmzx5IGoXNZmPqeRB1+KT65Ikk5U3E9G/kh/KJUj0LnbM=@vger.kernel.org X-Gm-Message-State: AOJu0YzP7KI/ZqUbljZQjtvotUiyGjB51feaXpRsd3sLR7Jm+3NccVxg r1dIxjGL0FHjRf/h8iX0wKfBKAHZUycFtpquNDyfj+SPkpiI4hQV X-Gm-Gg: ASbGncvMgmSChtz3Sfa3NeLJQp+015bryC5DsKsra+y9KZA910gWKSLGtnJlznKGFev 7mxlxDD1yyvgraqzocvIRo17QcCwCAsm7r8+hM0g9WeWVzxBDFzSXj6dRVvrGUs4C59s82wNc3n IasJEaH8KpcFn/4Ovkl5Me8d6z+Sph7dk7vcGQT7SZlxQuRmq07Q1PtdnieYrQkTeEO89HvKpKj pl87ImGjrw1aOnOWfEIY2pENIB8T0aZ3Ff95h+c2XeqgS35wahauniTs3YUTZo2FjIWAJDMWmL2 //ZvdaGbCt1fjUNZHUKLfyMOm8uRb3xak91NGZE63KOnsIA7TupoIpQzJzCPMNAm1c1FjmAVAg= = X-Google-Smtp-Source: AGHT+IEIZYO16EUwy5bWYTfuapkyReziPRqQOHrEE8d66FtEMAZjuctqsCd8Be6/ecyitm/MYSDiYA== X-Received: by 2002:a17:902:db0b:b0:221:89e6:ccb6 with SMTP id d9443c01a7336-2246454cf0fmr145304815ad.25.1741591862631; Mon, 10 Mar 2025 00:31:02 -0700 (PDT) Received: from vaxr-ASUSPRO-D840MB-M840MB.. ([2001:288:7001:2703:7ad0:37c7:5275:4b0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410a7f773sm70840715ad.117.2025.03.10.00.30.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 00:31:02 -0700 (PDT) From: I Hsin Cheng To: ojeda@kernel.org Cc: alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linux.dev, jserv@ccns.ncku.edu.tw, I Hsin Cheng Subject: [RFC PATCH 2/2] rust: list: Add examples for linked list Date: Mon, 10 Mar 2025 15:30:40 +0800 Message-ID: <20250310073040.423383-3-richard120310@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250310073040.423383-1-richard120310@gmail.com> References: <20250310073040.423383-1-richard120310@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add basic examples for the structure "List". They also serve as the unit tests for basic list methods. A simple "struct MyData" is used here to serve as the data to form the list, the members are trivial and naive for the simplicity of examples. The trait "ListArcSafe" isn't tracked here, dicussions are needed to see whether a tracker is necessary. Link: https://github.com/Rust-for-Linux/linux/issues/1121 Signed-off-by: I Hsin Cheng --- rust/kernel/list.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 57d75ca16434..d1954c403f67 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -35,6 +35,96 @@ /// * All prev/next pointers in `ListLinks` fields of items in the list ar= e valid and form a cycle. /// * For every item in the list, the list owns the associated [`ListArc`]= reference and has /// exclusive access to the `ListLinks` field. +/// +/// # Examples +/// +/// ``` +/// use kernel::list::*; +/// +/// struct MyData { value: i32, link: ListLinks<0> }; +/// +/// impl_list_arc_safe! { +/// impl ListArcSafe<0> for MyData { untracked; } +/// } +/// impl_has_list_links! { +/// impl HasListLinks<0> for MyData { self.link } +/// } +/// impl_list_item! { +/// impl ListItem<0> for MyData { using ListLinks; } +/// } +/// +/// let mut my_list =3D List::::new(); +/// +/// // The list should be empty at the moment. +/// assert!(my_list.is_empty()); +/// +/// let item_1 =3D ListArc::::new( +/// MyData { +/// value: 10, +/// link: ListLinks::<0>::new_link(), +/// }, GFP_KERNEL +/// ).unwrap(); +/// +/// let item_2 =3D ListArc::::new( +/// MyData { +/// value: 20, +/// link: ListLinks::<0>::new_link(), +/// }, GFP_KERNEL +/// ).unwrap(); +/// +/// let item_3 =3D ListArc::::new( +/// MyData { +/// value: 30, +/// link: ListLinks::<0>::new_link(), +/// }, GFP_KERNEL +/// ).unwrap(); +/// +/// // Append the nodes using push_back() +/// my_list.push_back(item_1); +/// my_list.push_back(item_2); +/// my_list.push_back(item_3); +/// +/// // Verify the length of the list. +/// assert_eq!(my_list.iter().count(), 3); +/// +/// // Iterater over the list and verify +/// // the nodes were inserted correctly. +/// let mut counter =3D 10; +/// for item in my_list.iter() { +/// assert_eq!(item.value, counter); +/// counter +=3D 10; +/// } +/// +/// // Pop the items out from the list and +/// // verify their content. +/// let item_3 =3D my_list.pop_back().unwrap(); +/// let item_1 =3D my_list.pop_front().unwrap(); +/// let item_2 =3D my_list.pop_front().unwrap(); +/// assert_eq!(item_1.value, 10); +/// assert_eq!(item_2.value, 20); +/// assert_eq!(item_3.value, 30); +/// assert!(my_list.is_empty()); +/// +/// // Append the nodes using push_front() +/// my_list.push_front(item_1); +/// my_list.push_front(item_2); +/// my_list.push_front(item_3); +/// assert_eq!(my_list.iter().count(), 3); +/// +/// // Use Cursor to verify the nodes were +/// // inserted correctly. +/// +/// let mut cursor =3D my_list.cursor_front().unwrap(); +/// assert_eq!(cursor.current().value, 30); +/// cursor =3D cursor.next().unwrap(); +/// assert_eq!(cursor.current().value, 20); +/// cursor =3D cursor.next().unwrap(); +/// assert_eq!(cursor.current().value, 10); +/// assert!(cursor.next().is_none()); +/// +/// # Ok::<(), Error>(()) +/// ``` +/// pub struct List, const ID: u64 =3D 0> { first: *mut ListLinksFields, _ty: PhantomData>, --=20 2.43.0