- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要写一个函数 foo
这需要一个 &RefCell<Box<dyn Any>>
,借自 RefCell
并返回一个向下转换的对象。向下转型的类型是在运行时选择的,但对于这个例子,我们假设它是 usize
.
use core::any::Any;
use std::cell::{RefCell, Ref};
pub fn foo<T: 'static>(cell: &RefCell<Box<dyn Any>>) -> Option<Ref<Box<T>>> {
???
}
pub fn main() {
let boxed: Box<dyn Any> = Box::new(1 as usize);
let cell = RefCell::new(boxed);
let num = foo(&cell);
println!("x: {}", num.unwrap());
}
我尝试实现
foo
像这样:
// 1:
pub fn foo<T: 'static>(cell: &RefCell<Box<dyn Any>>) -> Option<Ref<Box<T>>> {
let borrowed_cell = Ref::map(cell.borrow(), |borrow| borrow.downcast_ref::<T>().unwrap());
Some(borrowed_cell)
}
这个版本的问题在于它假设
downcast_ref
将始终有效,但我想捕获一个
downcast_ref
错误。
foo
以一种我可以捕获错误的方式:
// 2:
pub fn foo<T: 'static>(cell: &RefCell<Box<dyn Any>>) -> Option<Ref<T>> {
{
cell.borrow().downcast_ref::<T>()?;
}
let borrowed_cell = Ref::map(cell.borrow(), |borrow| borrow.downcast_ref::<T>().unwrap());
Some(borrowed_cell)
}
这个版本可以捕获downcast错误,但它必须调用
downcast_ref
两次(这可以接受,但我想知道是否有更好的方法)。尝试使用时
downcast_ref
只有一次,我陷入了一生的错误。
最佳答案
经过一番修改后,我想出了这个解决方案。您可以使用 Any::is<T>()
在映射之前检查借用。
pub fn foo<T: 'static>(cell: &RefCell<Box<dyn Any>>) -> Option<Ref<T>> {
let borrowed = cell.borrow();
if borrowed.is::<T>() {
Some(Ref::map(borrowed, |x| x.downcast_ref::<T>().unwrap()))
} else {
None
}
}
Rust Playground
link
关于rust - 将 Ref<Box<dyn Any>> 向下转换为 Ref<Box<T>> 时处理向下转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64310019/
考虑这样的代码: trait Foo { fn foo(&self); } fn consume_func(b: Box>) { unimplemented!(); } fn prod
考虑这样的代码: trait Foo { fn foo(&self); } fn consume_func(b: Box>) { unimplemented!(); } fn prod
我不明白在 Rust 中,当我们 Box 父结构时,结构中的结构发生了什么。 struct Outer1 { child: Inner1, } struct Inner1 { n: i
我将开始开发一个新网站,并准备处理浏览器用于计算元素宽度和高度的不同方法 (box model stuff)。不知何故,我想到了:如果我只是将 box-sizing 应用于网站中的所有元素会怎么样?
在 java 应用程序中使用 box java sdk 访问 box api 时,我遇到了下面提到的错误。请指出此问题的原因。 Exception in thread "main" com.box.s
计算盒子的宽高时,outline width like outline: 5px dashed red; 如果 box-sizing:border-box,框的宽度/高度是否会考虑轮廓? 最佳答案 我
读完the subtyping chapter of the Nomicon ,我无法理解类型参数的协方差。特别是对于 Box类型,描述为:T is covariant . 但是,如果我写这段代码:
给定这段代码: trait Trait {} struct Child; impl Trait for Child {} struct Father { child: &'a Box, } i
与文件 chooser for dropbox 类似,box.com 有没有? 最佳答案 Box 目前不维护自己的“文件选择器”;然而,有一个很棒的服务叫做 filepicker.io,它充当几乎所有
感谢您对我的问题的回复:Is this a bug of Box API v2 when getting events 这是一个与此相关的新问题。问题是我不能可靠地使用从以前的调用中获得的 next_
我试图让 Box Enterprise API 使用 As-User 工作。我有一个管理员帐户,我用它来尝试检索子帐户中的内容。 我首先使用管理员帐户检索子帐户的用户ID。并将用户 ID 添加为字段“
我想在一个简单的导轨中本地查看我的 Box 帐户中的图像 app .目标是使用这些图像进行幻灯片放映。我寻找必要的参数来传递每个图像相关 url成标签。看来我只能下载图像,或从 Box 应用程序中查看
这个问题在这里已经有了答案: Why doesn't Rust support trait object upcasting? (5 个答案) 关闭 4 年前。 给定代码 trait Base {
所以基本上我尝试从https://github.com/Gor-Ren/gym-jsbsim转换这个自定义健身房环境。使用farama基金会的gymnasium api。这是我正在处理的存储库:htt
我是BOX API的新手,因此正在使用API的v2。我正在从我的应用程序本地进行REST调用。 我想上传一个以前可能上传或未上传的文件。我知道父文件夹ID和文件名。我需要覆盖现有文件,或者至少要再
我已被授予访问(协作)文件夹的权限。我需要的是每天访问该文件夹并从中获取文件。现在我生成的开发者 token 将在 1 小时后过期。有什么办法可以得到authorization code没有第一站,它
我正在尝试将 Box 中上传的文件与另一个外部系统同步。我使用哪些 API 来获取“在给定 dd/mm/yy hh:mm:ss 后上传/更新的所有文件的列表”? 干杯! 最佳答案 我认为User Ev
Vagrant 文档和 CLI 使用术语“box”和“base box”。命名意味着基本框是一种特定类型的框,但是,这些术语似乎被用作同义词。 Vagrant“盒子”和 Vagrant“基础盒子”之间
Box 允许您为文件夹创建标签,但如何从 API 中获取该信息?我在 GET /folders/{id} API 中没有看到标签返回.标签也不被视为元数据。 最佳答案 如果你看看 Fields wri
我正在尝试确定应该使用哪个版本的 Box api。我的决定取决于 v1 api 的 EOL 时间线。我不想完成我的应用程序,就在应用程序发布之前(或之后不久),v1 api 停止工作。 我很乐意使用
我是一名优秀的程序员,十分优秀!