小编Hen*_*ler的帖子

有没有办法将泛型类型限制为几种类型之一?

我正在尝试创建一个泛型结构,它使用"整数类型"来引用数组.出于性能原因,我希望能够轻松指定是否使用u16,u32u64.像这样的东西(显然是无效的Rust代码):

struct Foo<T: u16 or u32 or u64> { ... }
Run Code Online (Sandbox Code Playgroud)

有没有办法表达这个?

rust

7
推荐指数
2
解决办法
1036
查看次数

Java堆内存分配限制

我编写了以下测试来检查最大可用堆内存:

import java.util.*;
public class Memory {
    public static void main(String[] args) {
        long maxMB = Runtime.getRuntime().maxMemory() / 1048576L;
        System.out.println("Maximum memory is " + maxMB + " MB.");
        ArrayList<byte[]> allocated = new ArrayList<>();
        try {
            while (true)
                allocated.add(new byte[1024*1024]);
        } catch (OutOfMemoryError e) {
            System.out.println("allocated " + allocated.size() + " MB before running out of memory.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我对此进行测试时,似乎实际上只能分配一半的“可用”内存:

$ java -Xmx512m Memory 
Maximum memory is 512 MB.
allocated 255 MB before running out of memory.
$ java -Xmx1024m …
Run Code Online (Sandbox Code Playgroud)

java memory

6
推荐指数
1
解决办法
547
查看次数

有没有办法让 Rust 推断关联类型的正确类型?

我正在尝试创建一个特征,其中包括在一些内部数据上返回迭代器的函数,然后可以由不同的结构实现。一个最小的例子如下:

trait WrapperTrait {
    type WrapperIterator: Iterator<Item=u32>;
    fn iter(&self) -> Self::WrapperIterator;
}
struct Wrapper {
    data: Vec<u32>
}
impl WrapperTrait for Wrapper {
    type WrapperIterator = ...;
    fn iter(&self) -> Self::WrapperIterator {
        return self.data.iter().map(|&x| x);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是, for 的类型WrapperIterator相当复杂 - 从错误消息(通过类型不匹配创建)我发现它是:

std::iter::Map<std::slice::Iter<'_, u32>, [closure@borrow.rs:45:37: 45:43]>
Run Code Online (Sandbox Code Playgroud)

有没有办法让 Rust 推断出正确的类型WrapperIterator

rust

5
推荐指数
1
解决办法
1279
查看次数

导出PartialEq时排除字段

是否有一种简单的方法来注释结构中的字段,以便在导出PartialEq特征时忽略它们?例如:

#[derive(PartialEq,Eq)]
pub struct UndirectedGraph {
    nodes: HashMap<NodeIdx, UndirectedNode>,
    // mapping of degree to nodes of that degree
    degree_index: Vec<HashSet<NodeIdx>>,
}
Run Code Online (Sandbox Code Playgroud)

我希望两个无向图在它们具有相同的nodes字段时被认为是相等的,但是degree_index字段可能不同(向量可能在末尾包含额外的空哈希集).

显然我可以手动实现特性,但自动推导会更简单.

rust

5
推荐指数
3
解决办法
452
查看次数

具有不同类型的可变参数的宏

我正在尝试创建宏,以便创建自定义树类型的小实例.为此,我想将子节点指定为节点或整数(没有明确地将它们转换为Node类型).我的尝试在下面,它与消息的$x解析类型一样失败.MDTreeexpected enum MDTree, found integral variable

pub struct MultiNode {
    children: Vec<MDTree>
}
impl MultiNode {
    pub fn new(children: Vec<MDTree>) -> Box<MultiNode> {
        return Box::new(MultiNode { children: children });
    }
}
pub enum MDTree {
    Single(u32),
    Prime(Box<MultiNode>),
    Degenerate(Box<MultiNode>),
}
macro_rules! mod_single {
    ($x:expr) => { MDTree::Single($x) }
}
macro_rules! mod_multi {
    ($($x:expr),*) => {{
        let mut children: Vec<MDTree> = Vec::new();
        $(
            match $x {
                0...4294967295 => { children.push(mod_single!($x)); }
                _ => { …
Run Code Online (Sandbox Code Playgroud)

macros rust

5
推荐指数
2
解决办法
611
查看次数

Scala - 用于不可变集合的hashCode缓存

似乎不可变的scala集合不会缓存它们的hashCode计算(针对immutable.HashSet进行测试),而是每次都重新计算它.是否有任何简单的方法来添加此行为(出于性能原因)?

我已经考虑过创建一个执行缓存的immutable.HashSet的子类,但是没有看到任何方法来实现+ etc.函数来返回一个缓存对象.尽管可能与授权有关,但这看起来非常难看.

caching scala scala-collections

4
推荐指数
1
解决办法
346
查看次数

通过32位整数索引向量

在Rust中,向量使用索引usize,因此在写入时

let my_vec: Vec<String> = vec!["Hello", "world"];
let index: u32 = 0;
println!("{}", my_vec[index]);
Run Code Online (Sandbox Code Playgroud)

你得到一个错误,因为索引应该是类型usize.我知道这可以通过显式转换索引来修复usize:

my_vec[index as usize]
Run Code Online (Sandbox Code Playgroud)

但这写起来很乏味.理想情况下,我只是[]通过实现来重载运算符

impl<T> std::ops::Index<u32> for Vec<T> { ... }
Run Code Online (Sandbox Code Playgroud)

但这是不可能的,因为Rust禁止这样做(因为特征和结构都不是本地的).我能看到的唯一选择是创建一个包装类Vec,但这意味着必须编写大量的函数包装器.有没有更优雅的方式来解决这个问题?

rust

4
推荐指数
1
解决办法
1753
查看次数

如何指定不需要其中一种类型的泛型类型?

我正在尝试使用通用数据类型,其中不需要其中一种类型(图中的边权重).我一直在考虑使用never类型,这看起来像这样:

#![feature(never_type)]

struct Foo<T> {
    bar: T
}

impl<T> Foo<T> {
    fn foo(&mut self, bar: T) {
        self.bar = bar;
    }
}

fn main() {
    let mut foo: Foo<!> = Foo { bar: "nada" };
    foo.foo("nada");
}
Run Code Online (Sandbox Code Playgroud)

这显然"nada"会导致占位符类型不匹配,但只输入任何内容都会导致其他错误.!这里使用的是正确的类型,如果是,那么正确的语法是什么?

我已经用它()代替了它!,但我有点不确定这是否是正确的类型选择.我相信在效率方面它应该没有区别,因为()没有内存占用?

rust

4
推荐指数
1
解决办法
183
查看次数

Kotlin 编译器找不到 import kotlin.test

我正在尝试通过命令行编译以下内容:

import kotlin.test.assertTrue
fun main(args: Array<String>) {
    assertTrue(false)
}
Run Code Online (Sandbox Code Playgroud)

但是,编译器失败并显示:

$ kotlinc -d MyCode.jar MyCode.kt 
MyCode.kt:1:15: error: unresolved reference: test
import kotlin.test.assertTrue
              ^
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

kotlin

2
推荐指数
1
解决办法
1420
查看次数

Rust:在默认方法实现中返回特征对象

当尝试从特征中的默认实现返回特征对象时,例如

trait Foo {
    fn foo(self: Box<Self>) -> Box<dyn Foo> {
        self
    }
}
Run Code Online (Sandbox Code Playgroud)

Rust 编译器抱怨说

error[E0277]: the size for values of type `Self` cannot be known at compilation time
note: required for the cast to the object type `dyn Foo`
Run Code Online (Sandbox Code Playgroud)

类型转换应该是 from Box<Self>to Box<dyn Foo>,为什么这需要调整 Self 的大小?

rust

2
推荐指数
1
解决办法
65
查看次数

实现特征时,"期望的类型参数,找到结构"

我正在尝试为有向图结构创建一个特征并提供一个非常基本的实现,但是遇到了编译器错误:

pub trait DiGraph<'a> {
    type N;
    fn nodes<T>(&'a self) -> T where T: Iterator<Item=&'a Self::N>;
    fn pre<T>(&'a self, node: Self::N) -> T where T: Iterator<Item=&'a Self::N>;
    fn succ<T>(&'a self, node: Self::N) -> T where T: Iterator<Item=&'a Self::N>;
}

struct SimpleNode {
    pre: Vec<usize>,
    succ: Vec<usize>,
}

pub struct SimpleDiGraph {
    pub nodes: Vec<SimpleNode>
}

impl<'a> DiGraph<'a> for SimpleDiGraph {
    type N = usize;

    fn nodes<T=std::ops::Range<usize>>(&'a self) -> T {
        return std::ops::Range { start: 0, end: self.nodes.len() };
    }
    fn …
Run Code Online (Sandbox Code Playgroud)

rust

1
推荐指数
1
解决办法
1667
查看次数

标签 统计

rust ×8

caching ×1

java ×1

kotlin ×1

macros ×1

memory ×1

scala ×1

scala-collections ×1