小编Ива*_*хин的帖子


为什么 function.toString() 输出“[native code]”,而登录到控制台直接显示函数的源代码?

我决定为 YouTube 实时聊天创建一个用户脚本。这是代码:

const toString = Function.prototype.toString

unsafeWindow.setTimeout = function (fn, t, ...args) {
    unsafeWindow.console.log(fn, fn.toString(), toString.call(fn))
    unsafeWindow.fns = (unsafeWindow.fns ?? []).concat(fn)
    return setTimeout(fn, t, ...args)
}
Run Code Online (Sandbox Code Playgroud)

现在看看输出的样子:

在此处输入图片说明

一些函数的输出是可以预测的,但看看其他函数!当你这样做console.log时,你会看到函数体,但如果你调用fn.toString(),你会看到function () { [native code] }

但为什么?脚本在页面之前加载,因此 YouTube 的脚本无法替换这些方法。

javascript function tostring console.log

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

为什么这样使用Condvar等待和通知不会死锁?

https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html

use std::sync::{Arc, Mutex, Condvar};
use std::thread;

let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = Arc::clone(&pair);

// Inside of our lock, spawn a new thread, and then wait for it to start.
thread::spawn(move|| {
    let (lock, cvar) = &*pair2;
    let mut started = lock.lock().unwrap(); // #1
    *started = true;
    // We notify the condvar that the value has changed.
    cvar.notify_one();
});

// Wait for the thread to start up.
let (lock, cvar) = &*pair;
let mut started = lock.lock().unwrap(); // …
Run Code Online (Sandbox Code Playgroud)

mutex rust conditional-variable

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

如何重新设置这个分支?

下图由两部分组成: 第一部分是存储库示例模型,第二部分是我想使用 git rebase 获取的存储库状态。

我没有忘记第二部分的任何内容。这正是我想要的。

在此处输入图片说明

git git-rebase

4
推荐指数
2
解决办法
46
查看次数

如何使用 Telethon 获取传入 Telegram 消息的聊天或组名称?

我有这个代码

from telethon.sync import TelegramClient, events

with TelegramClient('name', api_id, api_hash) as client:
   @client.on(events.NewMessage(pattern=pattern))
   async def handler(event):
      await event.reply("Here should be the Chat or Group name")
Run Code Online (Sandbox Code Playgroud)

如何实施?

python telegram telethon

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

我可以在 Rust 中将结构嵌入到枚举中吗?

我有这样的代码:

enum Packet {
    Quit,
    Message {
        text: String,
        time: i32,
        is_admin: bool,
    },
}
Run Code Online (Sandbox Code Playgroud)

这很方便,但我不喜欢像这样的嵌套结构。想象一下,如果我需要此枚举中的更多项目,那么数据包定义将太大。那么,有没有办法让我将 Message 结构移到外部,然后将其名称写在 Packet 结构定义中的某个位置?我想过做这样的事情:

struct ChatMessage {
    text: String,
    time: i32,
    is_admin: bool,
}
enum Packet {
    Quit,
    Message(ChatMessage),
}
Run Code Online (Sandbox Code Playgroud)

struct Message(顺便说一句,我可以将结构命名为与 Packet ( , )中的项目相同的名称吗Message(Message)?)
但是我必须let msg = message.0这样做或类似的事情。如果这是唯一的解决方案 - 我同意,但如果有更简洁的解决方案,我会很高兴。

rust

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

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

为什么 let 绑定中不允许使用顶级 or 模式?

fn foo(ok: bool) -> Result<i32, i32> {
    if ok { Ok(0) } else { Err(0) }
}

fn main() {
    let Ok(x) | Err(x) = foo(true); // rust-analyzer error: top-level or-patterns are not allowed in `let` bindings

    if let Ok(x) | Err(x) = foo(true) { // rust-analyzer warn: irrefutable `if let` pattern
        println!("Working!");
    }
}
Run Code Online (Sandbox Code Playgroud)

或者这是一个 Rust 分析器错误?我尝试用谷歌搜索但找不到任何东西。

rust rust-analyzer

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

Is there a way in Rust to overload method for a specific type?

The following is only an example. If there's a native solution for this exact problem with reading bytes - cool, but my goal is to learn how to do it by myself, for any other purpose as well.

I'd like to do something like this: (pseudo-code below)

let mut reader = Reader::new(bytesArr);
let int32: i32 = reader.read(); // separate implementation to read 4 bits and convert into int32
let int64: i64 = reader.read(); // separate implementation to read 8 bits …
Run Code Online (Sandbox Code Playgroud)

rust

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

我可以使用 typedef 进行类型断言吗?

我有一个自定义类型,可以让我更方便地使用 API:

type Object map[string]interface{}
Run Code Online (Sandbox Code Playgroud)

这件事不起作用:

var mockResponse = map[string]interface{}{"success": true}
resp, ok := mockResponse.(Object)
// ok = false
Run Code Online (Sandbox Code Playgroud)

我可以做任何事情,这样mockResponse.(Object)oktrue?基本上都是一样的类型。。。

go

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