From nobody Sun Feb 8 17:04:16 2026 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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 3EB4E3195EA for ; Wed, 24 Dec 2025 04:00:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766548837; cv=none; b=isBBSbs5TlRZ/mL/Ep7CC5QpIzYngvG8qC4W3SYq2qsq7mtJGLDP4Fcjbq+k9VHm4lof7hVm8cm3dH+/sXHNuVujjkh/egQzJ5arxjIQS6iTmo+0+Nv6fMbDWZKaUAEFUIibpBaC3Cz/xg897syIdzfoGKWcYXhQbhr5Xta2vQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766548837; c=relaxed/simple; bh=5dWflN3K5OTrsH0DdR1AIq8dxz4Dc+Sb5n+FhBK4TbQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ojyn2wdYAgJlEqSYhmXKoX+BBdCKt+qTwFG+r0d/IEDEa4+4CEyt9GZncrm7Fy+Ci+AYZ/155NyEudADlWywDGlJ9FiNKfZ2FB2V6dgzLBUcaoCdD7E/M0Jsg2PfgNGjCxRJleL2YaUNb0SbDwYLbzZtCwVyek3CxY4Hi9nefyI= 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=moKgfJdL; arc=none smtp.client-ip=209.85.216.54 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="moKgfJdL" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-34c24f4dfb7so4292658a91.0 for ; Tue, 23 Dec 2025 20:00:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766548834; x=1767153634; 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=82PHCBDnjWpHOS8UJkgNAZqGmK4UTjC/WbN+vRHAyTU=; b=moKgfJdL0PKflwM5Tk7vtoseJrwcPo6x1SU6XSbkXfaShPakwupyaniNCtCaMxjngD ny6KbTcWHB3JhiN9L+a/HQVSxmUc1aIKQYjk41uzEZYXZsUQY9gOugJ+1NXdWuIKwYrY 2Af1GoMQ68f8gsD4NN/YxizTINa8fQJ5m8lr7bCVnRQ5TIiX9lv8jCNGmd6Q9ffgZrWB GzxT3Z3zgvLkAme+s6I712Drkv8z1QSUZZAY2G3oJdpUle1Rg8e9fOiODpQCjFbb8Y7l faR++FtbvhHU3Geposdp48kSSX6UzjM2F5ZOkrwwB3V3ZHXnCX1kiMxV5of68d1VcrcW s9+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766548834; x=1767153634; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=82PHCBDnjWpHOS8UJkgNAZqGmK4UTjC/WbN+vRHAyTU=; b=D1Nbtec6Lr6pWXR3XMIzAjzOSb4GH5QQsrk852rLASPGVg6OtyxGoz6CLGk2G1OVvE 4t316hI01OHTOrQCm4oqLHoltteYUT2OCej/2X45d3BgdNwQ9H6yjCUcXGoFtPte9qIb iopGEvCzxKUbYAe56sDRLwzTny5GkSIqYqMNuXVd0lnz8awYbylIE+sgO6OY7buKKRaB RmhSNACo1Dv3VvsRmnQjC3/xq+gt2Au6Zea+GwCZEbVoJTiTLmHzjcu1OSO0Ey1Ar2s8 Sgsc0YFEqDv6JkIMZfN0o51rJfhttYIbkWCfqiqSrY4YfL8EhwN+SOO0XihCoI/10gTt 8Ipg== X-Forwarded-Encrypted: i=1; AJvYcCUxEDyOQ9Wt+WvmHWKVPSahB7YL4ruiDeoEUPQTuISolxT+75TfxqGBRXJL0i6sNunnkWFHxV71wIEO7qg=@vger.kernel.org X-Gm-Message-State: AOJu0YwM1ahY/vVivkYUIIvVGfXKFx370mnLi0Qan+WnFRekpcjnjHQT 23b1OsqOK3QvfBwCpLWyYA0sDTgOLPtYhPDfMhSah0xNp1h0qNDE6bYS X-Gm-Gg: AY/fxX6FEG+fbLpJ/XnZttVKBTcNp/xQolb+FClyuraS3g8wgmqjhqq3g8ml8AxWxq0 VWAbOuHvyQrNWewU3A6hMa22NK/ggEJbPIXOhnMQh4nOF0unbt23rroe3PpMRUsMjzJXMWSWUzW ZjEti0tuu3X2i637SAplN6+b8PCCC/72mDq1TPq3dfmP5prv8ukhASxlgdApsWqWb9RG4khKZ3d ge9FBxK8fTMRxbR7Jrg0/f3qJmtjPVAvJ66Xl9DBTPNy75fgIGLzKmEL4h2GQza3kwKg19MFVFL 5qrc6eHPfvIulajapIawgW/8TVZkuDTtd9rgDvAIUt/+MZ2yPc699A7B40LbyuJEmH80KbAMAnm pjqw+syIV+qs9cGUX/laUYoG30eomeWUySW5+uF6lMvYlrMRxdLKR27USzcc6s19sNogaZ/WDjt NmlEGa4d742/nfbIZMyuglqg== X-Google-Smtp-Source: AGHT+IEZc4P1j9t0ispVk5oXDUtPhHU67+CVystGtMT5XVU5WyvLofPU8JKlz1H1qwQkNieKhl7NYg== X-Received: by 2002:a17:90b:4b43:b0:340:e8e9:cc76 with SMTP id 98e67ed59e1d1-34e92144873mr14361360a91.11.1766548834347; Tue, 23 Dec 2025 20:00:34 -0800 (PST) Received: from localhost.localdomain ([104.28.249.217]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c1e7c5307c7sm13255131a12.28.2025.12.23.20.00.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Dec 2025 20:00:34 -0800 (PST) From: Jesung Yang To: Alexandre Courbot , Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: rust-for-linux@vger.kernel.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org, Jesung Yang Subject: [PATCH v3 4/4] rust: macros: add private doctests for `TryFrom` derive macro Date: Wed, 24 Dec 2025 03:59:57 +0000 Message-ID: <1b3de883a1ec17acffb818648512329298c400d3.1766544407.git.y.j3ms.n@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: 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 internal doctests to verify the `TryFrom` derive macro's logic. This ensures comprehensive testing while keeping the public-facing documentation compact and readable. Signed-off-by: Jesung Yang --- rust/macros/convert.rs | 579 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 579 insertions(+) diff --git a/rust/macros/convert.rs b/rust/macros/convert.rs index 7159fb4dd187..88a0f0b0df2d 100644 --- a/rust/macros/convert.rs +++ b/rust/macros/convert.rs @@ -965,3 +965,582 @@ mod overflow_assert_works_on_unsigned_int {} /// ``` mod any_into_target_overflow_is_rejected {} } + +mod derive_try_from_tests { + /// ``` + /// use kernel::{ + /// macros::{ + /// Into, + /// TryFrom, // + /// }, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, Into, PartialEq, TryFrom)] + /// #[into(bool, Bounded, Bounded, i8, i16, i32, i64, i1= 28, isize, u8, u16, u32, u64, u128, usize)] + /// #[try_from(bool, Bounded, Bounded, i8, i16, i32, i64= , i128, isize, u8, u16, u32, u64, u128, usize)] + /// enum Foo { + /// A, + /// B, + /// } + /// + /// assert_eq!(false, Foo::A.into()); + /// assert_eq!(true, Foo::B.into()); + /// assert_eq!(Ok(Foo::A), Foo::try_from(false)); + /// assert_eq!(Ok(Foo::B), Foo::try_from(true)); + /// + /// let foo_a: Bounded =3D Foo::A.into(); + /// let foo_b: Bounded =3D Foo::B.into(); + /// assert_eq!(Bounded::::new::<0>(), foo_a); + /// assert_eq!(Bounded::::new::<1>(), foo_b); + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<0>())= ); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<1>())= ); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<-1>(= ))); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<2>()= )); + /// + /// let foo_a: Bounded =3D Foo::A.into(); + /// let foo_b: Bounded =3D Foo::B.into(); + /// assert_eq!(Bounded::::new::<0>(), foo_a); + /// assert_eq!(Bounded::::new::<1>(), foo_b); + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<0>())= ); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<1>())= ); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<2>()= )); + /// + /// macro_rules! gen_signed_tests { + /// ($($type:ty),*) =3D> { + /// $( + /// assert_eq!(0 as $type, Foo::A.into()); + /// assert_eq!(1 as $type, Foo::B.into()); + /// assert_eq!(Ok(Foo::A), Foo::try_from(0 as $type)); + /// assert_eq!(Ok(Foo::B), Foo::try_from(1 as $type)); + /// assert_eq!(Err(EINVAL), Foo::try_from((0 as $type) - 1= )); + /// assert_eq!(Err(EINVAL), Foo::try_from((1 as $type) + 1= )); + /// )* + /// }; + /// } + /// macro_rules! gen_unsigned_tests { + /// ($($type:ty),*) =3D> { + /// $( + /// assert_eq!(0 as $type, Foo::A.into()); + /// assert_eq!(1 as $type, Foo::B.into()); + /// assert_eq!(Ok(Foo::A), Foo::try_from(0 as $type)); + /// assert_eq!(Ok(Foo::B), Foo::try_from(1 as $type)); + /// assert_eq!(Err(EINVAL), Foo::try_from((1 as $type) + 1= )); + /// )* + /// }; + /// } + /// gen_signed_tests!(i8, i16, i32, i64, i128, isize); + /// gen_unsigned_tests!(u8, u16, u32, u64, u128, usize); + /// ``` + mod works_with_derive_into {} + + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(u8)] + /// enum Foo { + /// // Works with const expressions. + /// A =3D add(0, 0), + /// B =3D 2_isize.pow(1) - 1, + /// } + /// + /// const fn add(a: isize, b: isize) -> isize { + /// a + b + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(0_u8)); + /// assert_eq!(Ok(Foo::B), Foo::try_from(1_u8)); + /// assert_eq!(Err(EINVAL), Foo::try_from(2_u8)); + /// ``` + mod works_with_const_expr {} + + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(bool)] + /// enum Foo { + /// A, + /// B, + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(false)); + /// assert_eq!(Ok(Foo::B), Foo::try_from(true)); + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(bool)] + /// enum Bar { + /// A, + /// } + /// + /// assert_eq!(Ok(Bar::A), Bar::try_from(false)); + /// assert_eq!(Err(EINVAL), Bar::try_from(true)); + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(bool)] + /// enum Baz { + /// A =3D 1, + /// } + /// + /// assert_eq!(Err(EINVAL), Baz::try_from(false)); + /// assert_eq!(Ok(Baz::A), Baz::try_from(true)); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(bool)] + /// enum Foo { + /// // `-1` cannot be represented with `bool`. + /// A =3D -1, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(bool)] + /// enum Foo { + /// // `2` cannot be represented with `bool`. + /// A =3D 2, + /// } + /// ``` + mod overflow_assert_works_on_bool {} + + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// A =3D -1 << 6, // The minimum value of `Bounded`. + /// B =3D (1 << 6) - 1, // The maximum value of `Bounded`. + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<{ -1_= i8 << 6 }>())); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<{ (1_= i8 << 6) - 1 }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ (-= 1_i8 << 6) + 1 }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ (1= _i8 << 6) - 2 }>())); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `1 << 6` cannot be represented with `Bounded`. + /// A =3D 1 << 6, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `(-1 << 6) - 1` cannot be represented with `Bounded`. + /// A =3D (-1 << 6) - 1, + /// } + /// ``` + /// + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// A =3D -1, // The minimum value of `Bounded`. + /// B, // The maximum value of `Bounded`. + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<{ -1_= i8 }>())); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<{ 0_i= 8 } >())); + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Bar { + /// A =3D -1, // The minimum value of `Bounded`. + /// } + /// + /// assert_eq!(Ok(Bar::A), Bar::try_from(Bounded::::new::<{ -1_= i8 }>())); + /// assert_eq!(Err(EINVAL), Bar::try_from(Bounded::::new::<{ 0_= i8 } >())); + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Baz { + /// A, // The maximum value of `Bounded`. + /// } + /// + /// assert_eq!(Err(EINVAL), Baz::try_from(Bounded::::new::<{ -1= _i8 }>())); + /// assert_eq!(Ok(Baz::A), Baz::try_from(Bounded::::new::<{ 0_i= 8 } >())); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `1` cannot be represented with `Bounded`. + /// A =3D 1, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `-2` cannot be represented with `Bounded`. + /// A =3D -2, + /// } + /// ``` + /// + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// #[repr(i64)] + /// enum Foo { + /// A =3D i32::MIN as i64, + /// B =3D i32::MAX as i64, + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<{ i= 32::MIN }>())); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<{ i= 32::MAX }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ = i32::MIN + 1 }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ = i32::MAX - 1 }>())); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// #[repr(i64)] + /// enum Foo { + /// // `1 << 31` cannot be represented with `Bounded`. + /// A =3D 1 << 31, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// #[repr(i64)] + /// enum Foo { + /// // `(-1 << 31) - 1` cannot be represented with `Bounded`. + /// A =3D (-1 << 31) - 1, + /// } + /// ``` + mod overflow_assert_works_on_signed_bounded {} + + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// A, // The minimum value of `Bounded`. + /// B =3D (1 << 7) - 1, // The maximum value of `Bounded`. + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<{ 0 }= >())); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<{ (1_= u8 << 7) - 1 }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ 1 = }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ (1= _u8 << 7) - 2 }>())); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `1 << 7` cannot be represented with `Bounded`. + /// A =3D 1 << 7, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `-1` cannot be represented with `Bounded`. + /// A =3D -1, + /// } + /// ``` + /// + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// A, // The minimum value of `Bounded`. + /// B, // The maximum value of `Bounded`. + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<{ 0 }= >())); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<{ 1 }= >())); + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Bar { + /// A, // The minimum value of `Bounded`. + /// } + /// + /// assert_eq!(Ok(Bar::A), Bar::try_from(Bounded::::new::<{ 0 }= >())); + /// assert_eq!(Err(EINVAL), Bar::try_from(Bounded::::new::<{ 1 = }>())); + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// enum Baz { + /// A =3D 1, // The maximum value of `Bounded`. + /// } + /// + /// assert_eq!(Err(EINVAL), Baz::try_from(Bounded::::new::<{ 0 = }>())); + /// assert_eq!(Ok(Baz::A), Baz::try_from(Bounded::::new::<{ 1 }= >())); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `2` cannot be represented with `Bounded`. + /// A =3D 2, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// enum Foo { + /// // `-1` cannot be represented with `Bounded`. + /// A =3D -1, + /// } + /// ``` + /// + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(Bounded)] + /// #[repr(u64)] + /// enum Foo { + /// A =3D u32::MIN as u64, + /// B =3D u32::MAX as u64, + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(Bounded::::new::<{ u= 32::MIN }>())); + /// assert_eq!(Ok(Foo::B), Foo::try_from(Bounded::::new::<{ u= 32::MAX }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ = u32::MIN + 1 }>())); + /// assert_eq!(Err(EINVAL), Foo::try_from(Bounded::::new::<{ = u32::MAX - 1 }>())); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// #[repr(u64)] + /// enum Foo { + /// // `1 << 32` cannot be represented with `Bounded`. + /// A =3D 1 << 32, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded)] + /// #[repr(u64)] + /// enum Foo { + /// // `-1` cannot be represented with `Bounded`. + /// A =3D -1, + /// } + /// ``` + mod overflow_assert_works_on_unsigned_bounded {} + + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(isize)] + /// #[repr(isize)] + /// enum Foo { + /// A =3D isize::MIN, + /// B =3D isize::MAX, + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(isize::MIN)); + /// assert_eq!(Ok(Foo::B), Foo::try_from(isize::MAX)); + /// assert_eq!(Err(EINVAL), Foo::try_from(isize::MIN + 1)); + /// assert_eq!(Err(EINVAL), Foo::try_from(isize::MAX - 1)); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(isize)] + /// #[repr(usize)] + /// enum Foo { + /// A =3D (isize::MAX as usize) + 1 + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(i32)] + /// #[repr(i64)] + /// enum Foo { + /// A =3D (i32::MIN as i64) - 1, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(i32)] + /// #[repr(i64)] + /// enum Foo { + /// A =3D (i32::MAX as i64) + 1, + /// } + /// ``` + mod overflow_assert_works_on_signed_int {} + + /// ``` + /// use kernel::{ + /// macros::TryFrom, + /// num::Bounded, + /// prelude::*, // + /// }; + /// + /// #[derive(Debug, PartialEq, TryFrom)] + /// #[try_from(usize)] + /// #[repr(usize)] + /// enum Foo { + /// A =3D usize::MIN, + /// B =3D usize::MAX, + /// } + /// + /// assert_eq!(Ok(Foo::A), Foo::try_from(usize::MIN)); + /// assert_eq!(Ok(Foo::B), Foo::try_from(usize::MAX)); + /// assert_eq!(Err(EINVAL), Foo::try_from(usize::MIN + 1)); + /// assert_eq!(Err(EINVAL), Foo::try_from(usize::MAX - 1)); + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(usize)] + /// #[repr(isize)] + /// enum Foo { + /// A =3D -1, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(u32)] + /// #[repr(i64)] + /// enum Foo { + /// A =3D (u32::MIN as i64) - 1, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(u32)] + /// #[repr(i64)] + /// enum Foo { + /// A =3D (u32::MAX as i64) + 1, + /// } + /// ``` + mod overflow_assert_works_on_unsigned_int {} + + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(Bounded, i8, i16, i32, i64)] + /// #[repr(i8)] + /// enum Foo { + /// // `i8::MAX` cannot be represented with `Bounded`. + /// A =3D i8::MAX, + /// } + /// ``` + /// + /// ```compile_fail + /// use kernel::macros::TryFrom; + /// + /// #[derive(TryFrom)] + /// #[try_from(i8, i16, i32, i64, Bounded)] + /// #[repr(i8)] + /// enum Foo { + /// // `i8::MAX` cannot be represented with `Bounded`. + /// A =3D i8::MAX, + /// } + /// ``` + mod any_try_from_target_overflow_is_rejected {} +} --=20 2.47.3