assert that Username does not impl PartialEq

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-08-10 12:21:12 +02:00
parent e7cb4dc50d
commit 14263ef989

View File

@ -76,6 +76,12 @@ pub const PROXMOX_AUTH_REALM_SCHEMA: Schema = PROXMOX_AUTH_REALM_STRING_SCHEMA.s
/// This alone does NOT uniquely identify the user and therefore does not implement `Eq`. In order /// This alone does NOT uniquely identify the user and therefore does not implement `Eq`. In order
/// to compare user names directly, they need to be explicitly compared as strings by calling /// to compare user names directly, they need to be explicitly compared as strings by calling
/// `.as_str()`. /// `.as_str()`.
///
/// ```compile_fail
/// fn test(a: Username, b: Username) -> bool {
/// a == b // illegal and does not compile
/// }
/// ```
#[derive(Clone, Debug, Hash, Deserialize, Serialize)] #[derive(Clone, Debug, Hash, Deserialize, Serialize)]
pub struct Username(String); pub struct Username(String);
@ -85,6 +91,26 @@ pub struct Username(String);
#[derive(Debug, Hash)] #[derive(Debug, Hash)]
pub struct UsernameRef(str); pub struct UsernameRef(str);
#[doc(hidden)]
/// ```compile_fail
/// let a: Username = unsafe { std::mem::zeroed() };
/// let b: Username = unsafe { std::mem::zeroed() };
/// let _ = <Username as PartialEq>::eq(&a, &b);
/// ```
///
/// ```compile_fail
/// let a: &UsernameRef = unsafe { std::mem::zeroed() };
/// let b: &UsernameRef = unsafe { std::mem::zeroed() };
/// let _ = <&UsernameRef as PartialEq>::eq(a, b);
/// ```
///
/// ```compile_fail
/// let a: &UsernameRef = unsafe { std::mem::zeroed() };
/// let b: &UsernameRef = unsafe { std::mem::zeroed() };
/// let _ = <&UsernameRef as PartialEq>::eq(&a, &b);
/// ```
struct _AssertNoEqImpl;
impl UsernameRef { impl UsernameRef {
fn new(s: &str) -> &Self { fn new(s: &str) -> &Self {
unsafe { &*(s as *const str as *const UsernameRef) } unsafe { &*(s as *const str as *const UsernameRef) }