Struct WslError

Source
pub struct WslError { /* private fields */ }

Implementations§

Source§

impl WslError

Source

pub fn hresult(&self) -> HRESULT

Source

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 Debug for WslError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for WslError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for WslError

Source§

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

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<(HRESULT, LXSS_ERROR_INFO)> for WslError

Source§

fn from(value: LxssError) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for WslError

Source§

fn from(value: Error) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.