【问题标题】:How can I represent this data?我如何表示这些数据?
【发布时间】:2019-11-07 10:47:24
【问题描述】:

我在此主题上的previous question 已被正确识别为XY problem. 的实例

我正在尝试创建一个资源管理箱来决定谁可以租用 flooble。公共界面(当前)看起来像这样:

pub struct Bid<T> {
    max_bid: TokenPerNanoSecond,
    budget: Token,
    data: T
}

/// Returns a vector of tuples of (T, time of rent end, tokens spent)
pub fn who_rents_the_flooble<'a, T>(
    mut bids: Vec<&'a mut Bid<T>>
) -> Vec<(T, NanoSecond, Token)> {
    let mut output = vec![];
    let mut now = NanoSecond::from(0);
    // while bids.len() > 0 {
        // run a mini auction to work out who wins
        // increment now by the duration
        // append winner's data, now and amount spent to output
        // subtract amount spent from winner's budget
        // remove bids with no budget left
    // }
    output
}

TokenNanoSecondTokenPerNanoSecondu64 的新类型,它们的算术关系已完全定义;它们主要是因为我不擅长代数而出现,并且不希望由于我的基本代数错误和语义不同数据的混淆而出现细微的错误。

T 是一个纯粹不透明的东西。它是 C 回调的void *,作为调用者识别输入和输出之间关系的一种方式。

但是,Vec&lt;&amp;'a mut Bid&lt;T&gt;&gt; 并没有真正做我需要它做的事情。为了实现“迷你拍卖”,我需要重新订购bidsVec&lt;&amp;'a Bid&lt;T&gt;&gt; 副本,这需要获得引用的所有权,并且当我下次需要改变@987654334 时会让我有点卡住@s.

我的架构应该是什么样的?


如果这还不够信息,请注意我正在尝试在 Rust 中重新实现 this bad code

【问题讨论】:

  • 为什么不接受Vec&lt;Bid&lt;T&gt;&gt;
  • @Shepmaster 大约2小时前我就想到了,它似乎已经解决了大部分问题。为了确定,我打算在写答案之前完成程序的编写。

标签: types architecture rust


【解决方案1】:

所有这些问题都可以通过简单地获得向量的所有权来解决;你的原型应该是这样的:

pub fn who_rents_the_flooble<T>(
    mut bids: Vec<Bid<T>>
) -> Vec<(T, NanoSecond, Token)>

通过值而不是通过引用来解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多