Skip to content

Error types everywhere instead of String #944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/animation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate sdl2;

use std::path::Path;

use sdl2::event::Event;
Expand All @@ -7,15 +8,15 @@ use sdl2::rect::Rect;
use sdl2::rect::Point;
use std::time::Duration;

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;

let window = video_subsystem.window("SDL2", 640, 480)
.position_centered().build().map_err(|e| e.to_string())?;
.position_centered().build()?;

let mut canvas = window.into_canvas()
.accelerated().build().map_err(|e| e.to_string())?;
.accelerated().build()?;
let texture_creator = canvas.texture_creator();

canvas.set_draw_color(sdl2::pixels::Color::RGBA(0,0,0,255));
Expand All @@ -27,8 +28,7 @@ fn main() -> Result<(), String> {
// animation sheet and extras are available from
// https://opengameart.org/content/a-platformer-in-the-forest
let temp_surface = sdl2::surface::Surface::load_bmp(Path::new("assets/characters.bmp"))?;
let texture = texture_creator.create_texture_from_surface(&temp_surface)
.map_err(|e| e.to_string())?;
let texture = texture_creator.create_texture_from_surface(&temp_surface)?;

let frames_per_anim = 4;
let sprite_tile_size = (32,32);
Expand Down
6 changes: 3 additions & 3 deletions examples/audio-capture-and-replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl AudioCallback for Recording {
}
}

fn record(audio_subsystem: &AudioSubsystem, desired_spec: &AudioSpecDesired) -> Result<Vec<i16>, String> {
fn record(audio_subsystem: &AudioSubsystem, desired_spec: &AudioSpecDesired) -> Result<Vec<i16>, Box<dyn std::error::Error>> {
println!("Capturing {:} seconds... Please rock!", RECORDING_LENGTH_SECONDS);

let (done_sender, done_receiver) = mpsc::channel();
Expand Down Expand Up @@ -100,7 +100,7 @@ impl AudioCallback for SoundPlayback {
}
}

fn replay_recorded_vec(audio_subsystem: &AudioSubsystem, desired_spec: &AudioSpecDesired, recorded_vec: Vec<i16>) -> Result<(), String> {
fn replay_recorded_vec(audio_subsystem: &AudioSubsystem, desired_spec: &AudioSpecDesired, recorded_vec: Vec<i16>) -> Result<(), Box<dyn std::error::Error>> {
println!("Playing...");

let playback_device = audio_subsystem.open_playback(None, desired_spec, |spec| {
Expand All @@ -121,7 +121,7 @@ fn replay_recorded_vec(audio_subsystem: &AudioSubsystem, desired_spec: &AudioSpe
}


fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let audio_subsystem = sdl_context.audio()?;

Expand Down
2 changes: 1 addition & 1 deletion examples/audio-queue-squarewave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn gen_wave(bytes_to_write: i32) -> Vec<i16> {
result
}

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let audio_subsystem = sdl_context.audio()?;

Expand Down
2 changes: 1 addition & 1 deletion examples/audio-squarewave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl AudioCallback for SquareWave {
}
}

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let audio_subsystem = sdl_context.audio()?;

Expand Down
2 changes: 1 addition & 1 deletion examples/audio-wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl AudioCallback for Sound {
}
}

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let wav_file : Cow<'static, Path> = match std::env::args().nth(1) {
None => Cow::from(Path::new("./assets/sine.wav")),
Some(s) => Cow::from(PathBuf::from(s))
Expand Down
2 changes: 1 addition & 1 deletion examples/audio-whitenoise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl AudioCallback for MyCallback {
}
}

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let audio_subsystem = sdl_context.audio()?;

Expand Down
9 changes: 4 additions & 5 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::surface::Surface;

pub fn run(png: &Path) -> Result<(), String> {
pub fn run(png: &Path) -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG)?;
let window = video_subsystem.window("rust-sdl2 demo: Cursor", 800, 600)
.position_centered()
.build()
.map_err(|e| e.to_string())?;
.build()?;

let mut canvas = window.into_canvas().software().build().map_err(|e| e.to_string())?;
let mut canvas = window.into_canvas().software().build()?;

let surface = Surface::from_file(png)
.map_err(|err| format!("failed to load cursor image: {}", err))?;
Expand Down Expand Up @@ -53,7 +52,7 @@ pub fn run(png: &Path) -> Result<(), String> {
}


fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<_> = env::args().collect();

if args.len() < 2 {
Expand Down
7 changes: 3 additions & 4 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;

pub fn main() -> Result<(), String> {
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;

let window = video_subsystem.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.opengl()
.build()
.map_err(|e| e.to_string())?;
.build()?;

let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;
let mut canvas = window.into_canvas().build()?;

canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas.clear();
Expand Down
7 changes: 3 additions & 4 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;

pub fn main() -> Result<(), String> {
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;

let window = video_subsystem.window("rust-sdl2 demo: Events", 800, 600)
.position_centered()
.resizable()
.build()
.map_err(|e| e.to_string())?;
.build()?;

let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;
let mut canvas = window.into_canvas().build()?;

canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas.clear();
Expand Down
2 changes: 1 addition & 1 deletion examples/game-controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate sdl2;

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let game_controller_subsystem = sdl_context.game_controller()?;

Expand Down
15 changes: 7 additions & 8 deletions examples/game-of-life-unsafe-textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ mod game_of_life {
}

#[cfg(feature = "unsafe_textures")]
fn dummy_texture<'a>(canvas: &mut Canvas<Window>) -> Result<(Texture, Texture), String> {
fn dummy_texture<'a>(canvas: &mut Canvas<Window>) -> Result<(Texture, Texture), Box<dyn std::error::Error>> {
enum TextureColor {
Yellow,
White,
};
let mut square_texture1 = canvas.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE).map_err(|e| e.to_string())?;
let mut square_texture2 = canvas.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE).map_err(|e| e.to_string())?;
let mut square_texture1 = canvas.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE)?;
let mut square_texture2 = canvas.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE)?;
// let's change the textures we just created
{
let textures = vec![
Expand Down Expand Up @@ -198,13 +198,13 @@ fn dummy_texture<'a>(canvas: &mut Canvas<Window>) -> Result<(Texture, Texture),
}
}
}
}).map_err(|e| e.to_string())?;
})?;
}
Ok((square_texture1, square_texture2))
}

#[cfg(feature = "unsafe_textures")]
pub fn main() -> Result<(), String> {
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;

Expand All @@ -217,15 +217,14 @@ pub fn main() -> Result<(), String> {
SQUARE_SIZE*PLAYGROUND_WIDTH,
SQUARE_SIZE*PLAYGROUND_HEIGHT)
.position_centered()
.build()
.map_err(|e| e.to_string())?;
.build()?;

// the canvas allows us to both manipulate the property of the window and to change its content
// via hardware or software rendering. See CanvasBuilder for more info.
let mut canvas = window.into_canvas()
.target_texture()
.present_vsync()
.build().map_err(|e| e.to_string())?;
.build()?;

println!("Using SDL_Renderer \"{}\"", canvas.info().name);
canvas.set_draw_color(Color::RGB(0, 0, 0));
Expand Down
16 changes: 7 additions & 9 deletions examples/game-of-life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ mod game_of_life {
}
}

fn dummy_texture<'a>(canvas: &mut Canvas<Window>, texture_creator: &'a TextureCreator<WindowContext>) -> Result<(Texture<'a>, Texture<'a>), String> {
fn dummy_texture<'a>(canvas: &mut Canvas<Window>, texture_creator: &'a TextureCreator<WindowContext>) -> Result<(Texture<'a>, Texture<'a>), Box<dyn std::error::Error>> {
enum TextureColor {
Yellow,
White,
};
let mut square_texture1 = texture_creator.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE).map_err(|e| e.to_string())?;
let mut square_texture2 = texture_creator.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE).map_err(|e| e.to_string())?;
let mut square_texture1 = texture_creator.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE)?;
let mut square_texture2 = texture_creator.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE)?;
// let's change the textures we just created
{
let textures = vec![
Expand Down Expand Up @@ -187,12 +187,12 @@ fn dummy_texture<'a>(canvas: &mut Canvas<Window>, texture_creator: &'a TextureCr
}
}
}
}).map_err(|e| e.to_string())?;
})?;
}
Ok((square_texture1, square_texture2))
}

pub fn main() -> Result<(), String> {
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;

Expand All @@ -205,16 +205,14 @@ pub fn main() -> Result<(), String> {
SQUARE_SIZE*PLAYGROUND_WIDTH,
SQUARE_SIZE*PLAYGROUND_HEIGHT)
.position_centered()
.build()
.map_err(|e| e.to_string())?;
.build()?;

// the canvas allows us to both manipulate the property of the window and to change its content
// via hardware or software rendering. See CanvasBuilder for more info.
let mut canvas = window.into_canvas()
.target_texture()
.present_vsync()
.build()
.map_err(|e| e.to_string())?;
.build()?;

println!("Using SDL_Renderer \"{}\"", canvas.info().name);
canvas.set_draw_color(Color::RGB(0, 0, 0));
Expand Down
7 changes: 3 additions & 4 deletions examples/gfx-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ use sdl2::gfx::primitives::DrawRenderer;
const SCREEN_WIDTH: u32 = 800;
const SCREEN_HEIGHT: u32 = 600;

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsys = sdl_context.video()?;
let window = video_subsys.window("rust-sdl2_gfx: draw line & FPSManager", SCREEN_WIDTH, SCREEN_HEIGHT)
.position_centered()
.opengl()
.build()
.map_err(|e| e.to_string())?;
.build()?;

let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;
let mut canvas = window.into_canvas().build()?;

canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
canvas.clear();
Expand Down
5 changes: 2 additions & 3 deletions examples/haptic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate sdl2;

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let joystick_subsystem = sdl_context.joystick()?;
let haptic_subsystem = sdl_context.haptic()?;
Expand All @@ -22,8 +22,7 @@ fn main() -> Result<(), String> {
},
}).expect("Couldn't open any joystick");

let mut haptic = haptic_subsystem.open_from_joystick_id(joystick_index)
.map_err(|e| e.to_string())?;
let mut haptic = haptic_subsystem.open_from_joystick_id(joystick_index)?;

for event in sdl_context.event_pump()?.wait_iter() {
use sdl2::event::Event;
Expand Down
9 changes: 4 additions & 5 deletions examples/image-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use sdl2::image::{LoadTexture, InitFlag};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;

pub fn run(png: &Path) -> Result<(), String> {
pub fn run(png: &Path) -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG)?;
let window = video_subsystem.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.build()
.map_err(|e| e.to_string())?;
.build()?;

let mut canvas = window.into_canvas().software().build().map_err(|e| e.to_string())?;
let mut canvas = window.into_canvas().software().build()?;
let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture(png)?;

Expand All @@ -36,7 +35,7 @@ pub fn run(png: &Path) -> Result<(), String> {
Ok(())
}

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<_> = env::args().collect();

if args.len() < 2 {
Expand Down
5 changes: 2 additions & 3 deletions examples/joystick.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate sdl2;

fn main() -> Result<(), String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let joystick_subsystem = sdl_context.joystick()?;

Expand All @@ -22,8 +22,7 @@ fn main() -> Result<(), String> {
}).expect("Couldn't open any joystick");

// Print the joystick's power level
println!("\"{}\" power level: {:?}", joystick.name(), joystick.power_level()
.map_err(|e| e.to_string())?);
println!("\"{}\" power level: {:?}", joystick.name(), joystick.power_level()?);

let (mut lo_freq, mut hi_freq) = (0, 0);

Expand Down
5 changes: 2 additions & 3 deletions examples/keyboard-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use sdl2::keyboard::Keycode;
use std::collections::HashSet;
use std::time::Duration;

pub fn main() -> Result<(), String> {
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;

let _window = video_subsystem.window("Keyboard", 800, 600)
.position_centered()
.build()
.map_err(|e| e.to_string())?;
.build()?;

let mut events = sdl_context.event_pump()?;

Expand Down
Loading