1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

// Copyright 2017 The gltf Library Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use import;
use std::ops;

use futures::{Future, Poll};
use std::boxed::Box;
use std::sync::Arc;

/// Represents a contiguous subset of either `AsyncData` or concrete `Data`.
#[derive(Clone, Copy, Debug)]
enum Region {
    /// Represents the whole contents of the parent data. 
    Full,

    /// Represents a subset of the contents of the parent data.
    View {
        /// Byte offset where the data region begins.
        offset: usize,

        /// Byte length past the offset where the data region ends.
        len: usize,
    },
}

/// A `Future` that drives the acquisition of glTF data.
pub struct Async<S: import::Source> {
    /// A `Future` that resolves to either a `SharedItem<Box<[u8]>>` or else an
    /// `AsyncError`.
    future: Box<Future<Item = Box<[u8]>, Error = import::Error<S>>>,

    /// The subset the data that is required once available.
    region: Region,
}

/// Concrete and thread-safe glTF data.
///
/// May represent `Buffer`, `View`, or `Image` data.
#[derive(Clone, Debug)]
pub struct Data {
    /// The resolved data.
    item: Arc<Box<[u8]>>,

    /// The byte region the data reads from.
    region: Region,
}

impl<S: import::Source> Async<S> {
    /// Constructs `AsyncData` that uses all data from the given future. 
    pub fn full(future: Box<Future<Item = Box<[u8]>, Error = S::Error>>) -> Self {
        Async {
            future: Box::new(future.map_err(import::Error::Source)),
            region: Region::Full,
        }
    }

    /// Constructs `AsyncData` that uses a subset of the data from the given future.
    pub fn view(
        future: Box<Future<Item = Box<[u8]>, Error = S::Error>>,
        offset: usize,
        len: usize,
    ) -> Self {
        Async {
            future: Box::new(future.map_err(import::Error::Source)),
            region: Region::View { offset, len },
        }
    }

    /// Consumes this `AsyncData`, constructing a subset instead.
    ///
    /// If the data is already a subset then a sub-subset is created, etc.
    pub fn subview(self, offset: usize, len: usize) -> Self {
        Async {
            future: self.future,
            region: self.region.subview(offset, len),
        }
    }
}

impl<S: import::Source> Future for Async<S> {
    type Item = Data;
    type Error = import::Error<S>;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.future
            .poll()
            .map(|async| {
                async.map(|item| {
                    match self.region {
                        Region::Full => {
                            Data::full(Arc::new(item))
                        },
                        Region::View { offset, len } => {
                            Data::view(Arc::new(item), offset, len)
                        },
                    }
                })
            })
    }
}

impl Data {
    /// Constructs concrete and thread-safe glTF data.
    ///
    /// # Notes
    ///
    /// This method is unstable and hence subject to change.
    pub fn full(item: Arc<Box<[u8]>>) -> Self {
        Data {
            item: item,
            region: Region::Full,
        }
    }

    /// Constructs a concrete and thread-safe subset of glTF data.
    ///
    /// # Notes
    ///
    /// This method is unstable and hence subject to change.
    pub fn view(item: Arc<Box<[u8]>>, offset: usize, len: usize) -> Self {
        Data {
            item: item,
            region: Region::View { offset, len },
        }
    }

    /// Consumes this `Data`, constructing a subset instead.
    ///
    /// If the data is already a subset then a sub-subset is created, etc.
    pub fn subview(self, offset: usize, len: usize) -> Self {
        Data {
            item: self.item,
            region: self.region.subview(offset, len),
        }
    }
}

impl ops::Deref for Data {
    type Target = [u8];
    fn deref(&self) -> &Self::Target {
        match self.region {
            Region::Full => &self.item[..],
            Region::View { offset, len } => &self.item[offset..(offset + len)],
        }
    }
}

impl Region {
    /// Consumes this `Region`, constructing a view instead.
    ///
    /// If the region is already a view then a subview is created, etc.
    pub fn subview(self, offset: usize, len: usize) -> Region {
        match self {
            Region::Full => {
                Region::View {
                    offset: offset,
                    len: len,
                }
            },
            Region::View {
                offset: prev_offset,
                len: _,
            } => {
                Region::View {
                    offset: prev_offset + offset,
                    len: len,
                }
            },
        }
    }
}