使用 Rust 的 u32 让 PostgreSQL 实现 MySQL 的 INT UNSIGNED

在上一章我们讨论到 PostgreSQL 没有 UNSIGNED 的问题,本章我们试图通过 rust 的 u32 来映射 PostgreSQL 的 int。

i32u32 的转换

无论 Rust 的 i32 还是 PostgreSQL 的 INT,当它们的值达到取值范围的最大值时,它们会溢出为最小值。

请看代码:

#[test]
fn test_max_i32_plus_1() {
    let max_i32 = i32::max_value();
    let min_i32 = i32::min_value();

    let from_max_i32 = (max_i32 as u32) + 1;
    let from_min_i32 = min_i32 as u32;
    assert!(from_max_i32 == from_min_i32);
}

使用 Rust 的 u32 让 PostgreSQL 实现 MySQL 的 INT UNSIGNED

依赖

[dependencies]
tokio={version="1", features=["full"]}
sqlx = { version = "0.6", features = [ "runtime-tokio-native-tls" , "postgres" ] }

代码演示

要查看完整内容,请先登录