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

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

问题描述

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

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

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

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>>并没有真正做我需要做的事情.为了实施小型拍卖",我需要重新订购bidsVec<&'a Bid<T>>副本,这需要获得引用的所有权,当我下次需要更改 s.

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天全站免登陆