update code to use TryFrom

gui-branch
an 2019-04-11 21:38:33 -04:00
parent 959930cede
commit 11ebd77e6a
15 changed files with 24 additions and 18 deletions

View File

@ -145,8 +145,7 @@ macro_rules! rd_impl {
/// argument. The function's result has the `?` operator applied to it,
/// unless `OPTS` is `no_try`.
/// - `OPT`, if not one of the things listed above, may be `enum TYPE` to apply
/// an enumeration made by `c_enum!`, or `flag TYPE` to apply a bitfield made
/// by `bitflags!`.
/// `TryFrom<TYPE>`, or `flag TYPE` to apply a bitfield made by `bitflags!`.
/// - `INDEX` is either an integer literal which must be representable as
/// `usize`, or a range with the syntax `INDEX; SIZE` denoting the beginning
/// and size of the range.

View File

@ -6,7 +6,7 @@
/// The syntax is similar to the `bitflags` macro, but each value has the
/// syntax `value => enumeration`. `enum` is used instead of `struct`.
///
/// This will generate an `enum $t` as well as a function `$t::try_from` which
/// This will generate an `enum $t` as well as implement `TryFrom` on it which
/// will return `Result<$t, ReprError>`.
///
/// # Examples
@ -49,10 +49,13 @@ macro_rules! c_enum
$($en = $va,)+
}
impl $t
#[allow(unused_qualifications)]
impl std::convert::TryFrom<$ti> for $t
{
type Error = ReprError;
/// Returns, if representable, the variant of `Self` from `n`.
$vi fn try_from(n: $ti) -> Result<Self, ReprError>
fn try_from(n: $ti) -> Result<Self, Self::Error>
{
match n {
$($va => Ok($t::$en),)+

View File

@ -21,19 +21,12 @@ pub const fn null_mut_void() -> *mut c_void {null_mut()}
impl CStringVec
{
/// Creates a new empty CStringVec.
#[inline]
pub fn new() -> Self
{
Self{sv: Vec::new(), cv: vec![null()]}
}
/// Creates a new `CStringVec` from an iterator.
#[inline]
pub fn new_from_iter<'a, I: Iterator<Item = &'a str>>(it: I)
-> ResultS<Self>
pub fn new_from_iter<'a, I>(it: I) -> ResultS<Self>
where I: Iterator<Item = &'a str>
{
let mut v = Self::new();
let mut v = Self::default();
for st in it {
v.push(CString::new(st)?);
@ -61,8 +54,9 @@ impl CStringVec
impl Default for CStringVec
{
/// Creates a new empty CStringVec.
#[inline]
fn default() -> Self {Self::new()}
fn default() -> Self {Self{sv: Vec::new(), cv: vec![null()]}}
}
/// An owned null-terminated string vector.

View File

@ -3,6 +3,7 @@
use super::{ltfn, TICKS_PER_SECOND};
use crate::durandal::{err::*, fixed::Fixed};
use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a `LITE` chunk.
pub fn read(b: &[u8]) -> ResultS<(Light, usize)>

View File

@ -1,6 +1,7 @@
//! `LightFunc` type.
use crate::durandal::{err::*, fixed::Fixed};
use std::convert::TryFrom;
/// Reads a `LightFunc` object.
pub fn read(b: &[u8]) -> ResultS<LightFunc>

View File

@ -6,6 +6,7 @@ use crate::{durandal::{bin::OptU16,
fixed::{Angle, Fixed, Unit}},
marathon::xfer::TransferMode};
use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a `medi` chunk.
pub fn read(b: &[u8]) -> ResultS<(Media, usize)>

View File

@ -4,6 +4,7 @@ use super::pnts;
use crate::{durandal::{bin::OptU16, err::*, fixed::Unit},
marathon::xfer::TransferMode};
use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a polygon for either M1 or M2.
pub fn read_poly_inter(b: &[u8]) -> ResultS<Polygon>

View File

@ -4,6 +4,7 @@ use super::stex;
use crate::{durandal::{bin::OptU16, err::*, fixed::Fixed},
marathon::xfer::TransferMode};
use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a `SIDS` chunk.
pub fn read(b: &[u8]) -> ResultS<(Side, usize)>

View File

@ -2,6 +2,7 @@
use crate::durandal::{err::*, fixed::Fixed};
use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a `Damage` object.
pub fn read(b: &[u8]) -> ResultS<Damage>

View File

@ -1,6 +1,7 @@
//! `Monster` type.
use crate::durandal::{bin::OptU16, err::*, fixed::Unit};
use std::convert::TryFrom;
/// Reads a `Trigger` object.
pub fn read(b: &[u8]) -> ResultS<Trigger>

View File

@ -2,6 +2,7 @@
use crate::durandal::{bin::OptU16, err::*, fixed::Fixed};
use bitflags::bitflags;
use std::convert::TryFrom;
use super::trig;

View File

@ -1,6 +1,7 @@
//! QuickDraw PICT format loader.
use crate::durandal::{bin::*, err::*, image::*};
use std::convert::TryFrom;
// Reads a `PixMap` header.
fn read_pm_header<'a>(b: &'a [u8],

View File

@ -3,6 +3,7 @@
use crate::{durandal::{bin::*, err::*, fixed::*, image::*},
marathon::{text::*, xfer::TransferMode}};
use bitflags::bitflags;
use std::convert::TryFrom;
// Reads a color from a color table into `clut`.
fn read_color(b: &[u8], clut: &mut [ColorShp]) -> ResultS<()>

View File

@ -2,7 +2,7 @@
use crate::durandal::{bin::*, err::*, fixed::*, sound::*};
use bitflags::bitflags;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, convert::TryFrom};
/// Reads a sound.
pub fn read_sound(b: &[u8]) -> ResultS<Sound16>

View File

@ -2,7 +2,7 @@
use crate::{durandal::{bin::*, err::*, image},
marathon::{map, phy, pict, text::mac_roman_cstr}};
use std::collections::BTreeMap;
use std::{collections::BTreeMap, convert::TryFrom};
/// Reads all chunks in an entry.
pub fn read_chunks(b: &[u8], old: bool, siz_cnk: usize) -> ResultS<Vec<Chunk>>