pub fn getadjacentresolution(&self, isplus: bool) -> Option<&Resolution> { let len =…
- User
pub fn get_adjacent_resolution(&self, is_plus: bool) -> Option<&Resolution> { let len = self.valid_resolutions.len(); if len == 0 { return None; }
// 查找当前分辨率的索引 let current_index = self .valid_resolutions .iter() .position(|r| r == &self.current)?; let offset = if is_plus { 1 } else { -1 }; // 计算循环索引 let new_index = ((current_index as isize + offset + len as isize) % len as isize) as usize; self.valid_resolutions.get(new_index) } 我的这个方法如何 Assistant
你的方法看起来很好,但有一些小的改进可以使代码更清晰和安全。以下是改进后的代码:
pub fn get_adjacent_resolution(&self, is_plus: bool) -> Option<&Resolution> { let len = self.valid_resolutions.len(); if len == 0 { return None; } // 查找当前分辨率的索引 let current_index = self .valid_resolutions .iter() .position(|r| r == &self.current)?; let offset = if is_plus { 1 } else { -1 }; // 计算循环索引 let new_index = (current_index as isize + offset + len as isize) % len as isize; let new_index = if new_index < 0 { new_index + len as isize } else { new_index } as usize; self.valid_resolutions.get(new_index) }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.