Return Result in examples instead of unwrap
This commit is contained in:
parent
869737dadc
commit
e8f2fbdaf0
3 changed files with 47 additions and 50 deletions
|
|
@ -1,13 +1,14 @@
|
||||||
use rand::distributions::{Alphanumeric, Distribution};
|
use rand::distributions::{Alphanumeric, Distribution};
|
||||||
use window_clipboard::Clipboard;
|
use window_clipboard::Clipboard;
|
||||||
use winit::{
|
use winit::{
|
||||||
|
error::EventLoopError,
|
||||||
event::{ElementState, Event, KeyEvent, WindowEvent},
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
event_loop::EventLoop,
|
event_loop::EventLoop,
|
||||||
keyboard::Key,
|
keyboard::Key,
|
||||||
window::WindowBuilder,
|
window::WindowBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), EventLoopError> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
let data: String = Alphanumeric
|
let data: String = Alphanumeric
|
||||||
|
|
@ -28,30 +29,28 @@ fn main() {
|
||||||
|
|
||||||
clipboard.write(data.clone()).unwrap();
|
clipboard.write(data.clone()).unwrap();
|
||||||
|
|
||||||
event_loop
|
event_loop.run(move |event, elwt| match event {
|
||||||
.run(move |event, elwt| match event {
|
Event::WindowEvent {
|
||||||
Event::WindowEvent {
|
event:
|
||||||
event:
|
WindowEvent::KeyboardInput {
|
||||||
WindowEvent::KeyboardInput {
|
event:
|
||||||
event:
|
KeyEvent {
|
||||||
KeyEvent {
|
logical_key: Key::Character(c),
|
||||||
logical_key: Key::Character(c),
|
state: ElementState::Released,
|
||||||
state: ElementState::Released,
|
..
|
||||||
..
|
},
|
||||||
},
|
..
|
||||||
..
|
},
|
||||||
},
|
..
|
||||||
..
|
} if c == "G" => {
|
||||||
} if c == "G" => {
|
let new_data = clipboard.read().expect("Read data");
|
||||||
let new_data = clipboard.read().expect("Read data");
|
assert_eq!(data, new_data, "Data is equal");
|
||||||
assert_eq!(data, new_data, "Data is equal");
|
println!("Data copied successfully!");
|
||||||
println!("Data copied successfully!");
|
}
|
||||||
}
|
Event::WindowEvent {
|
||||||
Event::WindowEvent {
|
event: WindowEvent::CloseRequested,
|
||||||
event: WindowEvent::CloseRequested,
|
window_id,
|
||||||
window_id,
|
} if window_id == window.id() => elwt.exit(),
|
||||||
} if window_id == window.id() => elwt.exit(),
|
_ => {}
|
||||||
_ => {}
|
})
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
use window_clipboard::Clipboard;
|
use window_clipboard::Clipboard;
|
||||||
use winit::{
|
use winit::{
|
||||||
|
error::EventLoopError,
|
||||||
event::{Event, WindowEvent},
|
event::{Event, WindowEvent},
|
||||||
event_loop::EventLoop,
|
event_loop::EventLoop,
|
||||||
window::WindowBuilder,
|
window::WindowBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), EventLoopError> {
|
||||||
let event_loop = EventLoop::new().unwrap();
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
let window = WindowBuilder::new()
|
let window = WindowBuilder::new()
|
||||||
|
|
@ -16,16 +17,14 @@ fn main() {
|
||||||
let clipboard =
|
let clipboard =
|
||||||
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");
|
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");
|
||||||
|
|
||||||
event_loop
|
event_loop.run(move |event, elwt| match event {
|
||||||
.run(move |event, elwt| match event {
|
Event::AboutToWait => {
|
||||||
Event::AboutToWait => {
|
println!("{:?}", clipboard.read());
|
||||||
println!("{:?}", clipboard.read());
|
}
|
||||||
}
|
Event::WindowEvent {
|
||||||
Event::WindowEvent {
|
event: WindowEvent::CloseRequested,
|
||||||
event: WindowEvent::CloseRequested,
|
window_id,
|
||||||
window_id,
|
} if window_id == window.id() => elwt.exit(),
|
||||||
} if window_id == window.id() => elwt.exit(),
|
_ => {}
|
||||||
_ => {}
|
})
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
use window_clipboard::Clipboard;
|
use window_clipboard::Clipboard;
|
||||||
use winit::{
|
use winit::{
|
||||||
|
error::EventLoopError,
|
||||||
event::{Event, WindowEvent},
|
event::{Event, WindowEvent},
|
||||||
event_loop::EventLoop,
|
event_loop::EventLoop,
|
||||||
window::WindowBuilder,
|
window::WindowBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), EventLoopError> {
|
||||||
let event_loop = EventLoop::new().unwrap();
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
let window = WindowBuilder::new()
|
let window = WindowBuilder::new()
|
||||||
|
|
@ -20,13 +21,11 @@ fn main() {
|
||||||
.write(String::from("Hello, world!"))
|
.write(String::from("Hello, world!"))
|
||||||
.expect("Write to clipboard");
|
.expect("Write to clipboard");
|
||||||
|
|
||||||
event_loop
|
event_loop.run(move |event, elwt| match event {
|
||||||
.run(move |event, elwt| match event {
|
Event::WindowEvent {
|
||||||
Event::WindowEvent {
|
event: WindowEvent::CloseRequested,
|
||||||
event: WindowEvent::CloseRequested,
|
window_id,
|
||||||
window_id,
|
} if window_id == window.id() => elwt.exit(),
|
||||||
} if window_id == window.id() => elwt.exit(),
|
_ => {}
|
||||||
_ => {}
|
})
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue