Java 写出Rust 的match 代码

Java 写出Rust 的match 代码 在Java中,我们可以使用switch语句来实现类似于Rust中match的功能。下面是一个示例,展示了如何在Java中使用switch来模拟Rust的match语法。 public enum ServiceType { RESUME_COMMUNICATION(1, "复课沟通"), CLASS_PLANNING(2, "班级规划"), STUDENT_PLANNING(3, "学员规划"), STUDENT_INVENTORY(4, "学员盘点"), RENEWAL_INVENTORY_LOCK(5, "续报盘点锁定"), SATISFACTION_SURVEY(6, "满意度问卷"); private final Integer code; private final String description; ServiceType(Integer code, String description) { this.code = code; this.description = description; } public Integer getCode() { return code; } public String getDescription() { return description; } public static ServiceType fromCode(Integer code) { for (ServiceType type : values()) { if (type.code.equals(code)) { return type; } } throw new IllegalArgumentException("Unknown service type code: " + code); } // 模拟 Rust 的 match 表达式 public <T> T match( java.

Rust GUI

Rust GUI 使用 gpui Render 和 Render Once 的区别 Render: 持续渲染模式 组件会在每一帧都重新渲染 适用于需要频繁更新的动态内容 消耗更多资源,但能保持实时更新 Render Once: 一次性渲染模式 组件只在初始化或状态变化时渲染 适用于静态内容或不频繁更新的组件 性能更好,资源消耗更少 在 GPUI 中选择合适的渲染模式可以优化应用性能和用户体验。 Entity 是什么 Entity 是 GPUI 框架中的一个智能指针类型,类似于 React 中的引用机制。它用于管理 UI 组件的生命周期和状态。 Entity 的主要用法 1. 创建 Entity Entity 通过 cx.new() 方法创建,接受一个闭包来初始化组件: pub fn view(window: &mut Window, cx: &mut App) -> Entity<Self> { cx.new(|cx| Self::new(window, cx)) } 2. 在结构体中存储 Entity Entity 常被用作结构体字段,用于持有子组件的引用: pub struct Example { root: Entity<ButtonStory>, } 3. 在构造函数中创建子组件 impl Example { pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { let root = ButtonStory::view(window, cx); Self { root } } 4.

锈迹片段

Rust Snippet 锈迹片段 如何调试Rust过程属性宏的代码 通过println!(),可以打印出宏展开后的代码。 fn generate_handler(internal: bool, input: TokenStream) -> Result<TokenStream> { let crate_name = utils::get_crate_name(internal); println!("crate_name: {:?}", crate_name); let item_fn = syn::parse::<ItemFn>(input)?; let (impl_generics, type_generics, where_clause) = item_fn.sig.generics.split_for_impl(); let vis = &item_fn.vis; let docs = item_fn .attrs .iter() .filter(|attr| attr.path().is_ident("doc")) .cloned() .collect::<Vec<_>>(); let ident = &item_fn.sig.ident; let call_await = if item_fn.sig.asyncness.is_some() { Some(quote::quote!(.await)) } else { None }; let def_struct = if !item_fn.sig.generics.params.is_empty() { let iter = item_fn .

Rust实现Http Web Server

用Rust实现WebSerer的第一篇 最近业余时间一直在学习Rust,也在尝试用Rust去造轮子。第一个轮子就是用Rust去实现Web服务器。Web服务器的核心流程就是Request 和 Response。 简单的总结就是解析请求,然后匹配到Server初始化的路由处理器,然后路由处理器处理完返回。 同步版本的程序 启动TCP服务 解析处理请求 匹配路由 返回响应 示范代码如下 use sync_core::server::Server; use sync_core::service::Service; fn main() { //trace log tracing_subscriber::fmt::init(); let mut server = Server::new(Service::new()); let route = sync_core::route::Route::new("GET".to_string(), "/hello".to_string(), || { "Hello World".to_string() }); // hello world 2 return int value let route2 = sync_core::route::Route::new("GET".to_string(), "/hello2".to_string(), || { //i32 value return 42.to_string() }); // push route to server server.service.routes.push(route); server.service.routes.push(route2); server.start(); } use crate::service::Service; use log::info; use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; pub struct Server { pub service: Service, } impl Server { pub fn new(service: Service) -> Self { Self { service } } pub fn start(&self) { let addr = "127.

WEB网关系列02-基于http协议实现流量的代理

基于http协议实现流量的代理 模式之间差异 直连模式 代理模式 直连模式 直连模式,也就是服务与服务之间直接请求调用,比如说我们正常在浏览器上面输入baidu,然后浏览器把百度相应的响应反馈回来的过程。 这个过程种,用户充当了service-A的角色,百度的服务器充当了service-B的角色。 代码还原以上过程,利用java的Java.net.HttpURLConnection类实现网络访问 除了HttpURLConnection还有很多java封装的包也能实现网络访问 通过common封装好HttpClient; 通过 Apache 封装好CloseableHttpClient; 通过SpringBoot-RestTemplate; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; public class DirectMode { public static void main(String[] args) { String api = "https://www.baidu.com"; HttpURLConnection connection = null; InputStream in = null; BufferedReader reader = null; try { //构造一个URL对象 URL url = new URL(api); //获取URLConnection对象 connection = (HttpURLConnection) url.openConnection(); //getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,所以在开发中不调用connect()也可以) in = connection.

事件日志埋点

通过自定义注解和AOP实现自定义事件日志埋点 注解和aop类 注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface EventLog { Action action() default Action.DEFAULT; boolean recordUserId() default true; boolean recordUserName() default true; boolean recordUserPhone() default true; boolean recordTime() default true; } aop @Slf4j @Aspect @Order(1) public class EventLogAspect { public EventLogAspect() { } @Pointcut("@annotation(com.xxxx.EventLog)") private void eventLogPoint() { } @Around("eventLogPoint() && @annotation(eventLog)") public Object around(ProceedingJoinPoint pjp, EventLog eventLog) throws Throwable { Object[] args = pjp.getArgs(); if (args[0] instanceof Action.Param){ Action.Param param = (Action.