我如何表示这些数据? [英] How can I represent this data?

查看:29
本文介绍了我如何表示这些数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关于此主题的上一个问题已被正确识别为XY 问题.

My previous question on this topic has been correctly identified as an instance of the XY problem.

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

I'm trying to create a resource management crate to decide who gets to rent the flooble. The public interface (currently) looks sort of like this:

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 的新类型,其算术关系完全定义;他们之所以出现,主要是因为我不擅长代数,并且不希望因为我的基本代数错误和语义不同数据的混淆而出现微妙的错误.

Token, NanoSecond and TokenPerNanoSecond are newtypes of u64 with their arithmetic relationships fully defined; they're mostly present just because I'm bad at algebra, and don't want subtle bugs popping up because of my basic algebra mistakes and conflation of semantically different data.

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

T is a purely opaque thing. It's the void * of C callbacks, acting as a way for the caller to identify the relationship between what went in and what's come out.

然而,Vec<&'a mut Bid<T>> 并没有真正做我需要它做的事情.为了实现迷你拍卖",我需要重新订购Vec<&'a Bid<T>>bids副本,这需要采取引用的所有权,当我下次需要改变 Bid 时会让我有点卡住.

However, Vec<&'a mut Bid<T>> doesn't really do what I need it to do. In order to implement the "mini auction", I need to re-order a Vec<&'a Bid<T>> copy of bids, which requires taking ownership of the references and will leave me a little bit stuck when I next need to mutate the Bids.

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

What should my architecture look like?

如果这还不够信息,请注意我正在尝试重新实现这个糟糕的代码在 Rust 中.

If this isn't enough information, note that I'm trying to re-implement this bad code in Rust.

推荐答案

所有这些问题都可以通过获得向量的所有权来解决;您的原型应如下所示:

All of these problems can be solved simply by taking ownership of the vector; your prototype should look like this:

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

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

Passing by value, not by reference, solves the problem.

这篇关于我如何表示这些数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆