我有一种情况,我有一个特殊工厂创建的对象树.这有点类似于DI容器,但并不完全相同.
对象的创建总是通过构造函数发生,并且对象是不可变的.
在给定的执行中可能不需要对象树的某些部分,应该懒惰地创建.因此构造函数参数应该只是按需创建的工厂.这看起来像是一份工作Lazy.
但是,对象创建可能需要访问慢资源,因此始终是异步的.(对象工厂的创建函数返回一个Task.)这意味着该创建函数Lazy需要是异步的,因此注入的类型需要Lazy<Task<Foo>>.
但我宁愿没有双层包装.我想知道是否有可能强迫一个Task懒惰,即创建一个Task保证在等待之前不执行的东西.据我所知,a Task.Run或者Task.Factory.StartNew可以随时开始执行(例如,如果来自池的线程是空闲的),即使没有什么可以等待它.
public class SomePart
{
// Factory should create OtherPart immediately, but SlowPart
// creation should not run until and unless someone actually
// awaits the task.
public SomePart(OtherPart eagerPart, Task<SlowPart> lazyPart)
{
EagerPart = eagerPart;
LazyPart = lazyPart;
}
public OtherPart EagerPart {get;}
public Task<SlowPart> LazyPart {get;}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码,允许您向上和向下滚动文本垫.每次滚动(即处理用户输入)时,打击垫都会按预期更新.但是,在按下第一个键之前没有显示任何内容,尽管我正在调用pad.refresh(),就像我在每个用户输入后一样.
我的代码看起来像这样:
def main(self,stdscr):
x,y = 20,150 # size of the window
u,a = 10,20 # where to place window - up,across
pad = curses.newpad(20,150) # nlines, ncols
pad_pos = 0
exit = False
pad.addstr(0,0,str(self.all_results))
while not exit:
pad.addstr(0,0,str(self.format_results()))
++ stdscr.refresh()
pad.refresh(pad_pos,10, u,a, x,y)
-- cmd = stdscr.getch()
++ cmd = pad.getch()
stdscr.nodelay(1)
+ pad.getch() - caused the screen not to update
+ stdscr.refresh() - no change
if cmd != -1:
+ pad.getch() - - caused the screen not to …Run Code Online (Sandbox Code Playgroud) 我正在使用Rust中的宏并希望进行嵌套扩展,即组合.
这是我写的代码:
macro_rules! nested {
(
$(arg $arg:ident;)*
$(fun $fun:ident;)*
) => {
$(
$fun($($arg),*);
)*
}
}
fn show1(a: i32, b: i32, c: i32) {
println!("show1: {} {} {}", a, b, c);
}
fn show2(a: i32, b: i32, c: i32) {
println!("show2: {} {} {}", a, b, c);
}
fn main() {
let a = 1;
let b = 2;
let c = 3;
nested! {
arg a;
arg b;
arg c;
fun show1;
fun show2;
}
}
Run Code Online (Sandbox Code Playgroud)
简单地说,以下断言是否会引发火灾?
template<typename T>
auto destructor()
{
return +[](void* p){
((T*)p)->~T();
};
}
assert(destructor<int>() != destructor<char>());
Run Code Online (Sandbox Code Playgroud)
标准似乎只是说lambda转换函数指针与lambda本身做同样的事情,然后你意识到简单的破坏类型都有无操作析构函数,因此是相同的.
我有一个类Foo引用多个其他类型的对象IBar。该类有一个方法fun,需要frob至少在其中一个上调用方法IBar。我想用模拟IBars编写一个测试来验证这个要求。我正在使用 GoogleMock。我目前有这个:
class IBar { public: virtual void frob() = 0; };
class MockBar : public IBar { public: MOCK_METHOD0(frob, void ()); };
class Foo {
std::shared_ptr<IBar> bar1, bar2;
public:
Foo(std::shared_ptr<IBar> bar1, std::shared_ptr<IBar> bar2)
: bar1(std::move(bar1)), bar2(std::move(bar2))
{}
void fun() {
assert(condition1 || condition2);
if (condition1) bar1->frob();
if (condition2) bar2->frob();
}
}
TEST(FooTest, callsAtLeastOneFrob) {
auto bar1 = std::make_shared<MockBar>();
auto bar2 = std::make_shared<MockBar>();
Foo foo(bar1, bar2);
EXPECT_CALL(*bar1, frob()).Times(AtMost(1)); …Run Code Online (Sandbox Code Playgroud) 我有一个函数,它接受一个输入流,处理它的数据,然后返回一些东西,基本上是一个更复杂的版本:
fn read_number_from_stream(input: &mut io::BufRead) -> io::Result<u32> {
// code that does something useful here
Ok(0)
}
Run Code Online (Sandbox Code Playgroud)
现在我想为这个函数编写一个测试.
#[test]
fn input_with_zero_returns_zero() {
let test_input = read_from_string("0\n");
assert_eq!(Ok(0), read_number_from_stream(test_input));
}
Run Code Online (Sandbox Code Playgroud)
我该如何实施read_from_string?Rust的旧版本显然提供了std::io::mem::MemReader,但整个std::io::mem模块似乎在更新版本的Rust中消失了(我使用的是不稳定的1.5分支).
我在java中编写了一个java程序,按字母顺序对字符串进行排序.问题是当它对具有罗马数字的字符串进行排序时,它将其视为字符并相应地进行排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SampleCustomSortApp
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
ArrayList<String> titles =new ArrayList();
titles.add("java");
titles.add("J-IV");
titles.add("A-V");
titles.add("J-V");
titles.add("J-IX");
titles.add("J-XX");
titles.add("J-X");
titles.add("J-I");
titles.add("J-II");
titles.add("datawarehouse");
titles.add("oracledba");
System.out.println("Before Sorting Elements Are :"+titles+"\n");
Collections.sort(titles, new MyCustomCompator());
System.out.println("After Sorting Elements Are :"+titles);
}
}
class MyCustomCompator implements Comparator
{
public int compare(Object s1,Object s2)
{
String one = (String)s1;
String two = (String)s2;
/* for ascending order */
if(one.compareTo(two)>0){
return 1;
}else{
return -1;
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个处理整数序列的函数.
fn process_one(n: u32) {}
fn process<II>(ii: II)
where
II: IntoIterator<Item = u32>,
{
for n in ii {
process_one(n);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望客户端能够在Vec<u32>不消耗它的情况下传递(process(&v)).不能使用此功能,因为<&Vec<u32> as IntoIterator>::Item是&u32; 我不得不通过v.iter().cloned(),这很烦人.
或者,我可以Item = &u32使用绑定和使用process_one(*n),但后来我有相反的问题.
我试图想办法写这个通用,但我无法弄清楚如何.据我所知,没有的AsRef,Borrow,ToOwned,或Deref工作.
我需要的是一种写这个的方法:
fn process<II>(ii: II)
where
II: IntoIterator<Item = MAGIC>, /* MORE MAGIC */
{
for n in ii {
process_one(MAGIC(n));
}
}
Run Code Online (Sandbox Code Playgroud)
以便所有这些编译:
fn test() {
let v: …Run Code Online (Sandbox Code Playgroud) 例如,如果在代码中编写这些语句:
char a[10];
char b[10];
cin>>a;
cin>>b;
Run Code Online (Sandbox Code Playgroud)
cin>>b;没有看到Enter键入后按下的键,例如Hello
但是当cin>>b;写入cin.get(b, 10);然后从先前的语句中cin.get(b, 10);读取Enter键时.