Struct Wsl2

Source
pub struct Wsl2 { /* private fields */ }
Expand description

A higher-level API for interacting with WSL2 through COM

Implementations§

Source§

impl Wsl2

Source

pub fn new() -> Result<Self, WslError>

Creates a new WSL API instance with a background COM thread

Examples found in repository?
examples/basic_usage.rs (line 69)
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}
Source

pub fn shutdown(self, force: bool) -> Result<(), WslError>

Shuts down WSL and closes this handle.

Examples found in repository?
examples/basic_usage.rs (line 165)
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}
Source

pub fn get_default_distribution(&self) -> Result<Uuid, WslError>

Gets the default distribution.

Examples found in repository?
examples/basic_usage.rs (line 90)
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}
Source

pub fn launch( &self, distro_guid: Uuid, command: &str, args: &[&str], cwd: Option<&str>, username: &str, ) -> Result<WslProcess, WslError>

Launches a Linux process in the specified WSL distribution. The process runs under the specified username and returns handles to stdin/stdout/stderr for communication.

Examples found in repository?
examples/basic_usage.rs (lines 7-13)
5fn run_command(wsl: &Wsl2, distro_uuid: uuid::Uuid) -> Result<(), Box<dyn std::error::Error>> {
6    println!("Running command...");
7    let result = wsl.launch(
8        distro_uuid,
9        "/bin/echo",
10        &["echo", "Hello, world!"],
11        None,
12        "root",
13    );
14    let mut process = {
15        match result {
16            Ok(process) => {
17                println!("Successfully ran command: {process:?}");
18                process
19            }
20            Err(e) => {
21                eprintln!("Failed to run command: {:?}", e);
22                return Err(e.into());
23            }
24        }
25    };
26
27    let stdout = process.stdout.take().unwrap();
28    let stderr = process.stderr.take().unwrap();
29
30    let stdout_thread = std::thread::spawn(move || {
31        let reader = std::io::BufReader::new(stdout);
32        for line in reader.lines() {
33            match line {
34                Ok(line) => println!("stdout: {}", line),
35                Err(e) => {
36                    eprintln!("Error reading stdout: {}", e);
37                    return;
38                }
39            }
40        }
41    });
42
43    let stderr_thread = std::thread::spawn(move || {
44        let reader = std::io::BufReader::new(stderr);
45        for line in reader.lines() {
46            match line {
47                Ok(line) => println!("stderr: {}", line),
48                Err(e) => {
49                    eprintln!("Error reading stderr: {}", e);
50                    return;
51                }
52            }
53        }
54    });
55
56    println!("Waiting for process to finish...");
57    let status = process.wait()?;
58    println!("Process finished with status: {status:?}");
59
60    stdout_thread.join().unwrap();
61    stderr_thread.join().unwrap();
62
63    Ok(())
64}
Source

pub fn enumerate_distributions(&self) -> Result<Vec<Distribution>, WslError>

Enumerates the distributions.

Examples found in repository?
examples/basic_usage.rs (line 102)
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}
Source

pub fn export_distribution( &self, distro_guid: Uuid, file: impl AsRawHandle, stderr: impl AsRawHandle, flags: ExportFlags, ) -> Result<(), WslError>

Exports a distribution.

Examples found in repository?
examples/basic_usage.rs (line 113)
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}
Source

pub fn register_distribution( &self, name: &str, version: Version, file: impl AsRawHandle, stderr: impl AsRawHandle, flags: ImportFlags, ) -> Result<(Uuid, String), WslError>

Registers a WSL distribution in the default location. Note that the distribution name must be unique.

Examples found in repository?
examples/basic_usage.rs (line 128)
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}
Source

pub fn set_version( &self, distribution: Uuid, version: Version, stderr: impl AsRawHandle, ) -> Result<(), WslError>

Examples found in repository?
examples/basic_usage.rs (line 142)
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 Drop for Wsl2

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Wsl2

§

impl !RefUnwindSafe for Wsl2

§

impl Send for Wsl2

§

impl !Sync for Wsl2

§

impl Unpin for Wsl2

§

impl !UnwindSafe for Wsl2

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, 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.