gpt4 book ai didi

rust - 约束 Rust 方法接受有效类型对的两个变量

转载 作者:行者123 更新时间:2023-12-03 11:32:11 25 4
gpt4 key购买 nike

我有一个包含一些公共(public)字段的结构,但有两个字段必须包含相关类型。我还想实现一个只允许组合有效对的 new 方法。这将大大改善结构人体工程学。

为了说明这一点。我有一个名为 Query 的结构,它具有以下字段:

#[derive(Debug)]
pub struct Query<V, U> {
pub value: V,
pub unit: U,
pub name: String,
}

value可以是TimeDistance类型,unit可以是TimeUnit DistanceUnit 类型。

#[derive(Debug)]
pub struct Time;

#[derive(Debug)]
pub enum TimeUnit {
Seconds,
Hours,
Days,
Weeks,
Months,
Years,
}

#[derive(Debug)]
pub struct Distance;

#[derive(Debug)]
pub enum DistanceUnit {
Meters,
Kilometers,
Miles,
}

拥有这样的结构使得实现新的泛型方法变得容易:

impl Query<Time, TimeUnit> {
pub fn new(value: Time, unit: TimeUnit, name: String) -> Self {
Self { value, unit, name }
}
}

impl Query<Distance, DistanceUnit> {
pub fn new(value: Distance, unit: DistanceUnit, name: String) -> Self {
Self { value, unit, name }
}
}

但是这会留下一个漏洞并允许手动创建此类结构:

/// Do not allow creating query with Time and DistanceUnit and vice-versa
fn do_not_allow_such_case() {
let _ = Query {
value: Time {},
unit: DistanceUnit::Kilometers,
name: "query".into(),
};
}

另一种可能是更好的方法是使不可能的状态无法表示,因此我通过引入具有允许对的枚举来重构结构:

#[derive(Debug)]
pub enum Pair {
Time(Time, TimeUnit),
Distance(Distance, DistanceUnit),
}

#[derive(Debug)]
pub struct Query2 {
pub query: Pair,
pub name: String,
}

我更喜欢这种方式,但是我很难像以前的情况那样实现方法。我的第一个想法是为先前声明的枚举实现 From 特性,并实现一个通用的 new 方法,我可以在其中使用这些 From 特性作为约束:

impl From<(Time, TimeUnit)> for Pair {
fn from(q: (Time, TimeUnit)) -> Self {
Self::Time(q.0, q.1)
}
}

impl From<(Distance, DistanceUnit)> for Pair {
fn from(q: (Distance, DistanceUnit)) -> Self {
Self::Distance(q.0, q.1)
}
}
#[derive(Debug)]
pub enum ValueEnum {
Time(Time),
Distance(Distance),
}

impl From<Time> for ValueEnum {
fn from(v: Time) -> Self {
Self::Time(v)
}
}

impl From<Distance> for ValueEnum {
fn from(v: Distance) -> Self {
Self::Distance(v)
}
}
#[derive(Debug)]
pub enum UnitEnum {
Time(TimeUnit),
Distance(DistanceUnit),
}

impl From<TimeUnit> for UnitEnum {
fn from(u: TimeUnit) -> Self {
Self::Time(u)
}
}

impl From<DistanceUnit> for UnitEnum {
fn from(u: DistanceUnit) -> Self {
Self::Distance(u)
}
}

这是我尝试实现方法,但它无法编译。

impl Query2 {
pub fn new<V, U>(value: V, unit: U, name: String) -> Self
where
V: Into<ValueEnum>,
U: Into<UnitEnum>,
/// No idea how to constrain the pair
{
Self {
/// This would not allow compiling
query: (value, unit).into(),
name,
}
}
}

有没有办法实现我想要的?

这是 Rust Playground 的链接:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=64ac6f5cb9b439e9943925c7ee8dd2e6

最佳答案

我会使用特征和关联类型

use std::fmt::Debug;
pub trait Value {
type Unit: Debug;
}

#[derive(Debug)]
pub struct Time;

impl Value for Time {
type Unit = TimeUnit;
}

#[derive(Debug)]
pub enum TimeUnit {
Seconds,
Hours,
Days,
Weeks,
Months,
Years,
}

#[derive(Debug)]
pub struct Query<T: Value> {
pub value: T,
pub unit: <T as Value>::Unit,
pub name: String,
}

impl<T: Value> Query<T> {
pub fn new(value: T, unit: <T as Value>::Unit, name: String) -> Self {
Self { value, unit, name }
}
}

fn main() {
let time_query: Query<Time> = Query::new(Time {}, TimeUnit::Seconds, "Seconds".to_owned());
}

Playground link

另一种更简单的耦合值及其单位的方法是使单位成为值的成员。这种方法的优点是使 API 变得更小,但缺点是单元类型基本上无法命名。

#[derive(Debug)]
pub struct Distance {
unit: DistanceUnit,
}

#[derive(Debug)]
pub enum DistanceUnit {
Meters,
Kilometers,
Miles,
}

#[derive(Debug)]
pub struct Query<T> {
pub value: T,
pub name: String,
}

impl Query<Distance> {
pub fn new(value: Distance, name: String) -> Self {
Self { value, name }
}
}
fn main() {
let time_query: Query<Distance> = Query::<Distance>::new(
Distance {
unit: DistanceUnit::Meters,
},
"Seconds".to_owned(),
);
}

Playground link

关于rust - 约束 Rust 方法接受有效类型对的两个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65960868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com