小编Luc*_*iel的帖子

Rust中这个奇怪的递归类型错误发生了什么?

我有一个名为的可迭代结构Join:

use std::iter::Peekable;

#[derive(Debug)]
pub struct Join<T, S> {
    container: T,
    separator: S,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinItem<T, S> {
    Element(T),
    Separator(S),
}

pub struct JoinIter<Iter: Iterator, Sep> {
    iter: Peekable<Iter>,
    sep: Sep,
    next_sep: bool,
}

impl<Iter: Iterator, Sep> JoinIter<Iter, Sep> {
    fn new(iter: Iter, sep: Sep) -> Self {
        JoinIter {
            iter: iter.peekable(),
            sep,
            next_sep: false,
        }
    }
}

impl<I: Iterator, S: Clone> Iterator for JoinIter<I, S> {
    type Item = JoinItem<I::Item, S>; …
Run Code Online (Sandbox Code Playgroud)

types type-inference reference rust

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

在列表理解中捕获并产生

我正在写一个生成器函数.我想知道是否有一个更好的(阅读:更多pythonic,理想情况下具有列表理解)方式来实现这样的事情:

generator = gen()
captures = []
for _ in xrange(x):
    foo = next(generator)
    directories.append(foo['name'])
    yield foo
Run Code Online (Sandbox Code Playgroud)

这里的关键是我不想捕获整个产量 - 返回的字典gen()很大,这就是我使用生成器的原因.不过,我确实需要捕捉所有'名字'.我觉得有一种方法可以通过列表理解来实现这一点,但我只是没有看到它.思考?

python iteration list-comprehension generator

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

## test]暗含`#[cfg(test)]`吗?

按照惯例,Rust中的单元测试有一个单独的模块,该模块有条件地用#[cfg(test)]以下代码编译:

#[cfg(test)]
mod tests {
    #[test]
    fn test1() { ... }

    #[test]
    fn test2() { ... }
}
Run Code Online (Sandbox Code Playgroud)

但是,我一直在使用一种更内联测试的样式:

pub fn func1() {...}

#[cfg(test)]
#[test]
fn test_func1() {...}

pub fn func2() {...}

#[cfg(test)]
#[test]
fn test_func2() {...}
Run Code Online (Sandbox Code Playgroud)

我的问题是,#[test]暗示#[cfg(test)]吗?也就是说,如果我用#[test]而不是标记我的测试功能#[cfg(test)],那么在非测试版本中它们是否仍然正确存在?

testing attributes unit-testing visibility rust

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

IPython !! 不工作

所以我开始在我的Mac上使用IPython.!! 运算符,它应该执行shell命令并将输出作为有用数据,正在生成语法错误.它似乎只是把它解释为(!(!ls)),然后吐出!ls:命令未找到.我不能谷歌惊叹号,我不知道还有什么地方可以转

python macos ipython

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

如何同步处理响应代码时,如何异步处理龙卷风中的流数据?

我的龙卷风API调用将调用另一个URL,然后将结果流回客户端.但是,如果内部URL返回错误代码,我想单独处理我自己的错误,并将错误内容流式传输到客户端.我现在拥有的是:

@web.asynchronous
@gen.coroutine
def get(self, job_id):
    url = ...
    client = httpclient.AsyncHTTPClient()

    # handle_chunk will forward received bytes to the client, allowing
    # other HTTP requests to be handled concurrently
    response = yield client.fetch(httpclient.HTTPRequest(
        url=url,
        streaming_callback=self._handle_chunk))
    self.finish()

def _handle_chunk(self, chunk):
    self.write(chunk)
    self.flush()
Run Code Online (Sandbox Code Playgroud)

如果响应代码在200系列中,我需要将其修改为仅启动转发块,但client.fetch在整个请求完成之前不会产生响应.知道怎么做吗?

python asynchronous http tornado

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

生成与集合中的字符串不匹配的字符串

一个奇怪的问题 - 如何生成一组与字符串中的任何字符串不匹配的字符串?我不想对字符串做任何假设.解决方案理想情况下是基于STL的,但并非必须如此

例:

vector<string> strings;
/*...*/
string unMatching = generateUnmatching(strings); //this is the function I want

assert(find(strings.begin(), strings.end(), unMatching) == strings.end());
Run Code Online (Sandbox Code Playgroud)

c++ string stl

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

使用CRTP转发构造函数

我正在使用带有CRTP的模板类来实现克隆模式,使用第二个模板参数Base来实现多级继承.当我尝试调用间接基类的构造函数时,我收到编译器错误.

class B
{
public:
    B() {} //trivial constructor
    virtual B* clone()=0;
};

template<class Base, class Derived>
class Clonable
    :public Base //weird, I know
{
public:
    virtual B* clone() {return new Derived(*this);}
};

class D1 : public Clonable<B, D1>
{
public:
    D1(int a); //non-trivial constructor. Different signature than B
};

class D2 : public Clonable<D1, D2>
{
public:
    D2(int a): D1(a) {} //compiler error here
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我遇到的唯一解决方案是在Cloneable中使用可变参数模板构造函数,但我的编译器(VC++ 11)尚未实现它们.

c++ clone crtp

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