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

View File

@ -6,7 +6,7 @@
/// The syntax is similar to the `bitflags` macro, but each value has the /// The syntax is similar to the `bitflags` macro, but each value has the
/// syntax `value => enumeration`. `enum` is used instead of `struct`. /// 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>`. /// will return `Result<$t, ReprError>`.
/// ///
/// # Examples /// # Examples
@ -49,10 +49,13 @@ macro_rules! c_enum
$($en = $va,)+ $($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`. /// 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 { match n {
$($va => Ok($t::$en),)+ $($va => Ok($t::$en),)+

View File

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

View File

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

View File

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

View File

@ -6,6 +6,7 @@ use crate::{durandal::{bin::OptU16,
fixed::{Angle, Fixed, Unit}}, fixed::{Angle, Fixed, Unit}},
marathon::xfer::TransferMode}; marathon::xfer::TransferMode};
use bitflags::bitflags; use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a `medi` chunk. /// Reads a `medi` chunk.
pub fn read(b: &[u8]) -> ResultS<(Media, usize)> 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}, use crate::{durandal::{bin::OptU16, err::*, fixed::Unit},
marathon::xfer::TransferMode}; marathon::xfer::TransferMode};
use bitflags::bitflags; use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a polygon for either M1 or M2. /// Reads a polygon for either M1 or M2.
pub fn read_poly_inter(b: &[u8]) -> ResultS<Polygon> 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}, use crate::{durandal::{bin::OptU16, err::*, fixed::Fixed},
marathon::xfer::TransferMode}; marathon::xfer::TransferMode};
use bitflags::bitflags; use bitflags::bitflags;
use std::convert::TryFrom;
/// Reads a `SIDS` chunk. /// Reads a `SIDS` chunk.
pub fn read(b: &[u8]) -> ResultS<(Side, usize)> pub fn read(b: &[u8]) -> ResultS<(Side, usize)>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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