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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use env::SubEnvironment;
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;

/// An interface for getting shell and function arguments.
pub trait ArgumentsEnvironment {
    /// The type of arguments this environment holds.
    type Arg: Clone;

    /// Get the name of the shell.
    fn name(&self) -> &Self::Arg;
    /// Get an argument at any index. Arguments are 1-indexed since the shell variable `$0`
    /// refers to the shell's name. Thus the first real argument starts at index 1.
    fn arg(&self, idx: usize) -> Option<&Self::Arg>;
    /// Get the number of current arguments, NOT including the shell name.
    fn args_len(&self) -> usize;
    /// Get all current arguments as a possibly owned slice.
    fn args(&self) -> Cow<[Self::Arg]>;
}

impl<'a, T: ?Sized + ArgumentsEnvironment> ArgumentsEnvironment for &'a T {
    type Arg = T::Arg;

    fn name(&self) -> &Self::Arg {
        (**self).name()
    }

    fn arg(&self, idx: usize) -> Option<&Self::Arg> {
        (**self).arg(idx)
    }

    fn args_len(&self) -> usize {
        (**self).args_len()
    }

    fn args(&self) -> Cow<[Self::Arg]> {
        (**self).args()
    }
}

impl<'a, T: ?Sized + ArgumentsEnvironment> ArgumentsEnvironment for &'a mut T {
    type Arg = T::Arg;

    fn name(&self) -> &Self::Arg {
        (**self).name()
    }

    fn arg(&self, idx: usize) -> Option<&Self::Arg> {
        (**self).arg(idx)
    }

    fn args_len(&self) -> usize {
        (**self).args_len()
    }

    fn args(&self) -> Cow<[Self::Arg]> {
        (**self).args()
    }
}

/// An interface for setting shell and function arguments.
pub trait SetArgumentsEnvironment: ArgumentsEnvironment {
    /// A collection of arguments to set.
    type Args;
    /// Changes the environment's arguments to `new_args` and returns the old arguments.
    fn set_args(&mut self, new_args: Self::Args) -> Self::Args;
}

impl<'a, T: ?Sized + SetArgumentsEnvironment> SetArgumentsEnvironment for &'a mut T {
    type Args = T::Args;

    fn set_args(&mut self, new_args: Self::Args) -> Self::Args {
        (**self).set_args(new_args)
    }
}

/// An interface for shifting positional shell and function arguments.
pub trait ShiftArgumentsEnvironment {
    /// Shift parameters such that the positional parameter `n` will hold
    /// the value of the positional parameter `n + amt`.
    ///
    /// If `amt == 0`, then no change to the positional parameters
    /// should be made.
    fn shift_args(&mut self, amt: usize);
}

impl<'a, T: ?Sized + ShiftArgumentsEnvironment> ShiftArgumentsEnvironment for &'a mut T {
    fn shift_args(&mut self, amt: usize) {
        (**self).shift_args(amt)
    }
}

macro_rules! impl_env {
    ($(#[$attr:meta])* pub struct $Env:ident, $Rc:ident) => {
        $(#[$attr])*
        #[derive(Debug, PartialEq, Eq)]
        pub struct $Env<T> {
            name: $Rc<T>,
            args: $Rc<Vec<T>>,
        }

        impl<T> $Env<T> {
            /// Constructs a new environment and initializes it with the name of the
            /// current process as the shell name, and no arguments.
            pub fn new() -> Self where T: From<String> {
                let name = ::std::env::current_exe().ok().and_then(|path| {
                    path.file_name().and_then(|os_str| os_str.to_str().map(|s| s.to_owned()))
                }).unwrap_or_default();

                Self::with_name(name.into())
            }

            /// Constructs a new environment and initializes it with the
            /// provided name and no arguments.
            pub fn with_name(name: T) -> Self {
                $Env {
                    name: name.into(),
                    args: Vec::new().into(),
                }
            }

            /// Constructs a new environment and initializes it with the
            /// provided name and positional arguments.
            pub fn with_name_and_args<I: IntoIterator<Item = T>>(name: T, args: I) -> Self {
                $Env {
                    name: name.into(),
                    args: args.into_iter().map(Into::into).collect::<Vec<_>>().into(),
                }
            }
        }

        impl<T: From<String>> Default for $Env<T> {
            fn default() -> Self {
                Self::new()
            }
        }

        impl<T> Clone for $Env<T> {
            fn clone(&self) -> Self {
                $Env {
                    name: self.name.clone(),
                    args: self.args.clone(),
                }
            }
        }

        impl<T> SubEnvironment for $Env<T> {
            fn sub_env(&self) -> Self {
                self.clone()
            }
        }

        impl<T: Clone> ArgumentsEnvironment for $Env<T>
        {
            type Arg = T;

            fn name(&self) -> &Self::Arg {
                &self.name
            }

            fn arg(&self, idx: usize) -> Option<&Self::Arg> {
                if idx == 0 {
                    Some(self.name())
                } else {
                    self.args.get(idx - 1)
                }
            }

            fn args_len(&self) -> usize {
                self.args.len()
            }

            fn args(&self) -> Cow<[Self::Arg]> {
                Cow::Borrowed(&self.args)
            }
        }

        impl<T: Clone> SetArgumentsEnvironment for $Env<T>
        {
            // FIXME(breaking): change this to VecDequeue for efficient shifts
            type Args = $Rc<Vec<T>>;

            fn set_args(&mut self, new_args: Self::Args) -> Self::Args {
                ::std::mem::replace(&mut self.args, new_args)
            }
        }

        impl<T: Clone> ShiftArgumentsEnvironment for $Env<T> {
            fn shift_args(&mut self, amt: usize) {
                if amt == 0 {
                    return;
                }

                if amt >= self.args.len() {
                    // Keep around the already allocated memory if we're the only owner.
                    if let Some(args) = $Rc::get_mut(&mut self.args) {
                        args.clear();
                        return;
                    }

                    // Otherwise just pretend we no longer have any arguments
                    self.args = $Rc::new(Vec::new());
                }

                if let Some(args) = $Rc::get_mut(&mut self.args) {
                    // FIXME: consider using an internal VecDeque for more
                    // efficient shift operations? This may require other costs
                    // from converting to/from Vec, or breaking changes to
                    // implemented interfaces...
                    //
                    // Did some benchmark testing with various strategies
                    // on removing elements from the front of a vec:
                    // (1) using a repeated `.remove(0)`
                    // (2) swapping elements up until the elements to pop off are
                    //     completely in the back, followed by `.truncate()`
                    // (3) unsafely dropping in place the elements to pop off
                    //     followed by an (overlapping) pointer copy operation
                    //     generally naive, no consideration of alignment, etc.
                    // (4) reversing the elements in place, calling `.truncate()`
                    //     and then reversing the result
                    //
                    // Tested with various arg sizes and amounts to pop. All
                    // the strategies were pretty comperable to each other
                    // given the variance, but the genral results are roughly as follows:
                    // (1) if amount == 1 it is as fast as any other strategy,
                    //     but for larger amounts it quickly fell behind
                    // (2) this strategy was pretty much the worst except when
                    //     a little better than (1)'s bad case
                    // (3) consistently first or tied for first, turns out
                    //     the underlying reverse operation is pretty well optimized
                    //     for various memory alignments and element sizes
                    // (4) usually in first or tied for first given the variance
                    //
                    // Overall given that (3) can get us as good performance as (4)
                    // without having to worry about unsafe pointer math, we'll
                    // choose it for our general case (and leverage any future
                    // optimizations added to the reversal implementation). For
                    // the case where amount == 1 we'll stick with (1) since
                    // that let's us do the smallest amount of work possible.
                    if amt == 1 {
                        args.remove(0);
                    } else {
                        let len_to_keep = args.len() - amt;

                        args.reverse();
                        args.truncate(len_to_keep);
                        args.reverse();
                    }

                    return;
                }

                // Since we're not the only owner we're forced to copy everything over.
                self.args = $Rc::new(
                    self.args[amt..].iter()
                        .cloned()
                        .collect()
                );
            }
        }
    };
}

impl_env!(
    /// An environment module for setting and getting shell and function arguments.
    ///
    /// Uses `Rc` internally. For a possible `Send` and `Sync` implementation,
    /// see `env::atomic::ArgsEnv`.
    pub struct ArgsEnv,
    Rc
);

impl_env!(
    /// An environment module for setting and getting shell and function arguments.
    ///
    /// Uses `Arc` internally. If `Send` and `Sync` is not required of the implementation,
    /// see `env::ArgsEnv` as a cheaper alternative.
    pub struct AtomicArgsEnv,
    Arc
);

#[cfg(test)]
mod tests {
    use RefCounted;
    use env::SubEnvironment;
    use super::*;

    #[test]
    fn test_name() {
        let name = "shell";
        let env = ArgsEnv::with_name(name.to_owned());
        assert_eq!(env.name(), name);
        assert_eq!(env.arg(0).unwrap(), name);

        // Name should not change in sub environments
        let env = env.sub_env();
        assert_eq!(env.name(), name);
        assert_eq!(env.arg(0).unwrap(), name);
    }

    #[test]
    fn test_sub_env_no_needless_clone() {
        let name = ::std::rc::Rc::new("shell");
        let args = vec!("one".into(), "two".into(), "three".into());
        let env = ArgsEnv::with_name_and_args(name, args.clone());

        let mut env = env.sub_env();
        assert!(env.name.get_mut().is_none());
        assert!(env.args.get_mut().is_none());
    }

    #[test]
    fn test_args() {
        let name = "shell";
        let args = vec!("one", "two", "three");
        let env = ArgsEnv::with_name_and_args(name, args.clone());

        assert_eq!(env.args_len(), args.len());

        assert_eq!(env.arg(0), Some(&name));
        assert_eq!(env.arg(1), Some(&args[0]));
        assert_eq!(env.arg(2), Some(&args[1]));
        assert_eq!(env.arg(3), Some(&args[2]));
        assert_eq!(env.arg(4), None);

        assert_eq!(env.args(), args);
    }

    #[test]
    fn test_set_args() {
        let args_old = vec!("1", "2", "3");
        let mut env = ArgsEnv::with_name_and_args("shell", args_old.clone());

        {
            let args_new = vec!("4", "5", "6");
            assert_eq!(env.args(), args_old);
            let prev = env.set_args(args_new.clone().into());
            assert_eq!(*prev, args_old);
            assert_eq!(env.args(), args_new);

            env.set_args(prev);
        }

        assert_eq!(env.args(), args_old);
    }

    #[test]
    fn test_shift_args() {
        let mut env = ArgsEnv::with_name_and_args("shell", vec!("1", "2", "3", "4", "5", "6"));
        let _copy = env.sub_env();

        env.shift_args(0);
        assert_eq!(env.args(), vec!("1", "2", "3", "4", "5", "6"));
        assert!(env.name.get_mut().is_none()); // No needless clone here

        env.shift_args(1);
        assert_eq!(env.args(), vec!("2", "3", "4", "5", "6"));

        env.shift_args(1);
        assert_eq!(env.args(), vec!("3", "4", "5", "6"));

        env.shift_args(2);
        assert_eq!(env.args(), vec!("5", "6"));

        env.shift_args(100);
        assert_eq!(env.args(), Vec::<&str>::new());
    }
}