首页 > 其他分享 >Rust Empty Type

Rust Empty Type

时间:2024-09-24 14:57:31浏览次数:21  
标签:Type void FromBehaviour Empty type event Rust Void


Rust Empty Type

(Jin Qing’s Column, Sep., 2024)

Crate void defines an empty type enum Void {}.

Empty type is an enum with no variants. Empty type can not be instantiated.
See: https://doc.rust-lang.org/nomicon/exotic-sizes.html

Void is used in statically impossible cases (type-level unreachability).
For instance, a return type of Result<T, Void> indicates that it always returns Ok.

Trait void::ResultVoidErrExt can be used to unwrap the result, statically indicating the unreachability.

impl<T> ResultVoidExt<T> for Result<T, Void> {
    /// Get the value out of an always-ok Result.
    ///
    /// Never panics, since it is statically known to be Ok.
    #[inline]
    fn void_unwrap(self) -> T {
        match self {
            Ok(val) => val,
            Err(e) => unreachable(e)
        }
    }
}

rust-libp2p\swarm\src\dummy.rs uses Void to indicate that FromBehaviour event is impossible.

impl crate::handler::ConnectionHandler for ConnectionHandler {
    type FromBehaviour = Void;
    ...

    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
        void::unreachable(event)
    }
...
}


标签:Type,void,FromBehaviour,Empty,type,event,Rust,Void
From: https://blog.51cto.com/u_16162321/12099795

相关文章

  • Rust Pin
    RustPin(JinQing’sColumn,Sep.,2024)From:https://doc.rust-lang.org/std/pin/index.htmlRustcanpindatalocationinmemory,thatmeansitsaddresscannotbechanged.Pinneddataguaranteesthememorylocationisalwaysvalid.Safecodecannotmovepin......
  • 华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
    ......
  • 2024/09/22:TypeScript 学习笔记二
    1、类型注解在TypeScript中,可以使用类型注解来明确标识类型。如:constgreeting:string='helloworld' 2、类型检查静态类型检查:在程序编译时进行——(两种静态类型检查模式:非严格类型检查【默认方式】;严格类型检查)动态类型检查:在程序运行时进行3、TypeScri......