Skip to content

Commit 595cab2

Browse files
committed
uefi: Revising function calls, names, and return types to better match standard convention
1 parent 56602da commit 595cab2

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

uefi-test-runner/src/proto/shell.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,25 @@ pub fn test_current_dir(shell: &ScopedProtocol<Shell>) {
100100
assert_eq!(cur_fs_str, expected_fs_str);
101101
}
102102

103-
/// Test `get_env()`, `get_envs()`, and `set_env()`
103+
/// Test `var()`, `vars()`, and `set_var()`
104104
pub fn test_env(shell: &ScopedProtocol<Shell>) {
105105
/* Test retrieving list of environment variable names */
106-
let mut cur_env_vec = shell.get_envs();
106+
let mut cur_env_vec = shell.vars();
107107
assert_eq!(cur_env_vec.next().unwrap(), cstr16!("path"),);
108108
// check pre-defined shell variables; see UEFI Shell spec
109109
assert_eq!(cur_env_vec.next().unwrap(), cstr16!("nonesting"),);
110-
let cur_env_vec = shell.get_envs();
110+
let cur_env_vec = shell.vars();
111111
let default_len = cur_env_vec.count();
112112

113113
/* Test setting and getting a specific environment variable */
114-
let cur_env_vec = shell.get_envs();
114+
let cur_env_vec = shell.vars();
115115
let test_var = cstr16!("test_var");
116116
let test_val = cstr16!("test_val");
117-
assert!(shell.get_env(test_var).is_none());
118-
let status = shell.set_env(test_var, test_val, false);
119-
assert_eq!(status, Status::SUCCESS);
117+
assert!(shell.var(test_var).is_none());
118+
let status = shell.set_var(test_var, test_val, false);
119+
assert!(status.is_ok());
120120
let cur_env_str = shell
121-
.get_env(test_var)
121+
.var(test_var)
122122
.expect("Could not get environment variable");
123123
assert_eq!(cur_env_str, test_val);
124124

@@ -129,7 +129,7 @@ pub fn test_env(shell: &ScopedProtocol<Shell>) {
129129
}
130130
}
131131
assert!(!found_var);
132-
let cur_env_vec = shell.get_envs();
132+
let cur_env_vec = shell.vars();
133133
let mut found_var = false;
134134
for env_var in cur_env_vec {
135135
if env_var == test_var {
@@ -138,24 +138,24 @@ pub fn test_env(shell: &ScopedProtocol<Shell>) {
138138
}
139139
assert!(found_var);
140140

141-
let cur_env_vec = shell.get_envs();
141+
let cur_env_vec = shell.vars();
142142
assert_eq!(cur_env_vec.count(), default_len + 1);
143143

144144
/* Test deleting environment variable */
145145
let test_val = cstr16!("");
146-
let status = shell.set_env(test_var, test_val, false);
147-
assert_eq!(status, Status::SUCCESS);
148-
assert!(shell.get_env(test_var).is_none());
146+
let status = shell.set_var(test_var, test_val, false);
147+
assert!(status.is_ok());
148+
assert!(shell.var(test_var).is_none());
149149

150-
let cur_env_vec = shell.get_envs();
150+
let cur_env_vec = shell.vars();
151151
let mut found_var = false;
152152
for env_var in cur_env_vec {
153153
if env_var == test_var {
154154
found_var = true;
155155
}
156156
}
157157
assert!(!found_var);
158-
let cur_env_vec = shell.get_envs();
158+
let cur_env_vec = shell.vars();
159159
assert_eq!(cur_env_vec.count(), default_len);
160160
}
161161

uefi/src/proto/shell/mod.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use crate::proto::unsafe_protocol;
66
use crate::{CStr16, Char16, Error, Result, Status, StatusExt};
7+
78
use core::marker::PhantomData;
89
use core::ptr;
910
use uefi_raw::protocol::shell::ShellProtocol;
@@ -28,17 +29,12 @@ impl<'a> Iterator for Vars<'a> {
2829
// We iterate a list of NUL terminated CStr16s.
2930
// The list is terminated with a double NUL.
3031
fn next(&mut self) -> Option<Self::Item> {
31-
let cur_start = self.inner;
32-
let mut cur_len = 0;
33-
unsafe {
34-
if *(cur_start) == Char16::from_u16_unchecked(0) {
35-
return None;
36-
}
37-
while *(cur_start.add(cur_len)) != Char16::from_u16_unchecked(0) {
38-
cur_len += 1;
39-
}
40-
self.inner = self.inner.add(cur_len + 1);
41-
Some(CStr16::from_ptr(cur_start))
32+
let s = unsafe { CStr16::from_ptr(self.inner) };
33+
if s.is_empty() {
34+
None
35+
} else {
36+
self.inner = unsafe { self.inner.add(s.num_chars() + 1) };
37+
Some(s)
4238
}
4339
}
4440
}
@@ -98,8 +94,8 @@ impl Shell {
9894
/// environment variable
9995
/// * `None` - If environment variable does not exist
10096
#[must_use]
101-
pub fn get_env(&self, name: &CStr16) -> Option<&CStr16> {
102-
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast();
97+
pub fn var(&self, name: &CStr16) -> Option<&CStr16> {
98+
let name_ptr: *const Char16 = name.as_ptr();
10399
let var_val = unsafe { (self.0.get_env)(name_ptr.cast()) };
104100
if var_val.is_null() {
105101
None
@@ -108,13 +104,9 @@ impl Shell {
108104
}
109105
}
110106

111-
/// Gets the list of environment variables
112-
///
113-
/// # Returns
114-
///
115-
/// * `Vec<env_names>` - Vector of environment variable names
107+
/// Gets an iterator over the names of all environment variables
116108
#[must_use]
117-
pub fn get_envs(&self) -> Vars {
109+
pub fn vars(&self) -> Vars<'_> {
118110
let env_ptr = unsafe { (self.0.get_env)(ptr::null()) };
119111
Vars {
120112
inner: env_ptr.cast::<Char16>(),
@@ -134,10 +126,10 @@ impl Shell {
134126
/// # Returns
135127
///
136128
/// * `Status::SUCCESS` - The variable was successfully set
137-
pub fn set_env(&self, name: &CStr16, value: &CStr16, volatile: bool) -> Status {
138-
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast();
139-
let value_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(value).cast();
140-
unsafe { (self.0.set_env)(name_ptr.cast(), value_ptr.cast(), volatile) }
129+
pub fn set_var(&self, name: &CStr16, value: &CStr16, volatile: bool) -> Result {
130+
let name_ptr: *const Char16 = name.as_ptr();
131+
let value_ptr: *const Char16 = value.as_ptr();
132+
unsafe { (self.0.set_env)(name_ptr.cast(), value_ptr.cast(), volatile) }.to_result()
141133
}
142134
}
143135

0 commit comments

Comments
 (0)