pub struct WslError { /* private fields */ }Implementations§
Source§impl WslError
impl WslError
pub fn hresult(&self) -> HRESULT
Sourcepub fn kind(&self) -> Option<WslErrorKind>
pub fn kind(&self) -> Option<WslErrorKind>
Examples found in repository?
examples/basic_usage.rs (line 74)
66fn main() -> Result<(), Box<dyn std::error::Error>> {
67 println!("Creating WSL API instance...");
68
69 let wsl = match Wsl2::new() {
70 Ok(wsl) => {
71 println!("WSL API instance created successfully");
72 wsl
73 }
74 Err(e) if e.kind() == Some(WslErrorKind::UnsupportedOperatingSystem) => {
75 eprintln!("WSL is not installed or enabled on this operating system");
76 return Ok(());
77 }
78 Err(e) if e.kind() == Some(WslErrorKind::UnsupportedWslVersion) => {
79 eprintln!("WSL version is not supported");
80 return Ok(());
81 }
82 Err(e) => {
83 eprintln!("Failed to create WSL1/2 API instance: {:?}", e);
84 eprintln!("This may be due to WSL2 not being installed or enabled");
85 return Err(e.into());
86 }
87 };
88
89 println!("Getting default distribution...");
90 let default_distro = match wsl.get_default_distribution() {
91 Ok(distro) => {
92 println!("Successfully retrieved default distribution: {:?}", distro);
93 distro
94 }
95 Err(e) => {
96 eprintln!("Failed to get default distribution: {:?}", e);
97 return Err(e.into());
98 }
99 };
100
101 println!("Enumerating distributions...");
102 match wsl.enumerate_distributions() {
103 Ok(distros) => println!("Successfully enumerated distributions: {:?}", distros),
104 Err(e) => {
105 eprintln!("Failed to enumerate distributions: {:?}", e);
106 return Err(e.into());
107 }
108 }
109
110 println!("Exporting distribution...");
111 let file = std::fs::File::create("distro.tar.gz").unwrap();
112 let (r, w) = std::io::pipe().unwrap();
113 let result = wsl.export_distribution(default_distro, file, w, ExportFlags::empty());
114 // Keep the read end alive until after the export completes
115 drop(r);
116 match result {
117 Ok(_) => println!("Successfully exported distribution"),
118 Err(e) => {
119 eprintln!("Failed to export distribution: {:?}", e);
120 return Err(e.into());
121 }
122 }
123
124 let file = std::fs::File::open("distro.tar.gz").unwrap();
125
126 println!("Registering distribution...");
127 let (r, w) = std::io::pipe().unwrap();
128 let result = wsl.register_distribution("test", Version::WSL2, file, w, ImportFlags::empty());
129 let guid_copy = match result {
130 Ok((guid, name)) => {
131 println!("Successfully registered distribution: {:?} {}", guid, name);
132 guid
133 }
134 Err(e) => {
135 eprintln!("Failed to register distribution: {:?}", e);
136 return Err(e.into());
137 }
138 };
139 drop(r);
140
141 println!("Setting version...");
142 let result = wsl.set_version(guid_copy, Version::WSL1, std::io::stderr());
143 match result {
144 Ok(_) => println!("Successfully set version"),
145 Err(e) => {
146 eprintln!("Failed to set version: {:?}", e);
147 return Err(e.into());
148 }
149 }
150
151 println!("Enumerating distributions...");
152 match wsl.enumerate_distributions() {
153 Ok(distros) => println!("Successfully enumerated distributions: {:?}", distros),
154 Err(e) => {
155 eprintln!("Failed to enumerate distributions: {:?}", e);
156 return Err(e.into());
157 }
158 }
159
160 run_command(&wsl, default_distro)?;
161
162 run_command(&wsl, guid_copy)?;
163
164 println!("Shutting down WSL...");
165 match wsl.shutdown(false) {
166 Ok(_) => println!("Successfully shut down WSL"),
167 Err(e) => {
168 eprintln!("Failed to shut down WSL: {:?}", e);
169 return Err(e.into());
170 }
171 }
172
173 println!("Example completed successfully");
174 Ok(())
175}Trait Implementations§
Source§impl Error for WslError
impl Error for WslError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Auto Trait Implementations§
impl !Sync for WslError
impl Freeze for WslError
impl RefUnwindSafe for WslError
impl Send for WslError
impl Unpin for WslError
impl UnsafeUnpin for WslError
impl UnwindSafe for WslError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more