我正在为一个不支持 std 或 alloc 的平台编写 Rust 代码,所以我只能使用 core。最近,我的代码开始产生此编译错误:
error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait
error: `#[alloc_error_handler]` function required, but not found
Run Code Online (Sandbox Code Playgroud)
有没有办法让 Rust 编译器告诉我为什么需要全局内存分配器?据推测,在我的代码或我的依赖项中的某个地方,某些东西正在尝试分配,这导致它出错,但我无法弄清楚这是在哪里发生的。
对于这种情况,我在网上看到的唯一建议是制作一个虚拟的自定义分配器,然后查看已编译的二进制文件以查找对其的引用。然而,我这样做了,但无法在它生成的二进制可执行文件中找到对我的自定义分配器的任何引用,这让我对发生的事情更加困惑。
我正在编写一种类型,我希望能够根据内部类型的类型类实例化来更改方法的行为。
举个例子,考虑一下:
class Reduceable a where
reduce :: a -> a
-- Other methods not relevant here
instance Reduceable [[a]] where
-- Reduce a list by dropping empty lists in the front
reduce = dropWhile null
-- Other methods implemented
instance Reduceable [a] => Reduceable [[a]] where
-- Reduce a list of reduceable things by dropping empty lists in the front
-- and also reducing the elements of the list
reduce = dropWhile null . map reduce
-- Other methods …Run Code Online (Sandbox Code Playgroud)