Skip to content

Commit 31728ad

Browse files
committed
Merge pull request #35 from zrneely/master
Remove Copy constraint in Actions, and document weaker Clone constraint
2 parents 2e16bc5 + 5f2d42e commit 31728ad

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

README.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,24 +253,25 @@ The following actions are available out of the box. They may be used in either
253253

254254
``Store``
255255
An option has single argument. Stores a value from command-line in a
256-
variable. Any type that has ``FromStr`` trait implemented may be used.
256+
variable. Any type that has the ``FromStr`` and ``Clone`` traits implemented
257+
may be used.
257258

258259
``StoreOption``
259260
As ``Store``, but wrap value with ``Some`` for use with ``Option``. For
260261
example:
261-
262+
262263
let mut x: Option<i32> = None;
263264
ap.refer(&mut x).add_option(&["-x"], StoreOption, "Set var x");
264265

265266
``StoreConst(value)``
266267
An option has no arguments. Store a hard-coded ``value`` into variable,
267-
when specified. Any type may be used.
268+
when specified. Any type with the ``Clone`` trait implemented may be used.
268269

269270
``PushConst(value)``
270271
An option has no arguments. Push a hard-coded ``value`` into variable,
271-
when specified. Any type may be used. Option might used for a list of
272-
operations to perform, when ``required`` is set for this variable, at least
273-
one operation is required.
272+
when specified. Any type which has the ``Clone`` type implemented may be
273+
used. Option might used for a list of operations to perform, when ``required``
274+
is set for this variable, at least one operation is required.
274275

275276
``StoreTrue``
276277
Stores boolean ``true`` value in a variable.
@@ -283,11 +284,11 @@ The following actions are available out of the box. They may be used in either
283284

284285
``IncrBy(num)``
285286
An option has no arguments. Increments the value stored in a variable by a
286-
value ``num``. Any type which has ``Add`` trait may be used.
287+
value ``num``. Any type which has the ``Add`` and ``Clone`` traits may be used.
287288

288289
``DecrBy(nym)``
289290
Decrements the value stored in a variable by a value ``num``. Any type
290-
which has ``Add`` trait may be used.
291+
which has the ``Add`` and ``Clone`` traits may be used.
291292

292293
``Collect``
293294
When used for an ``--option``, requires single argument. When used for a

src/generic.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ pub struct ListAction<'a, T: 'a> {
3131
cell: Rc<RefCell<&'a mut Vec<T>>>,
3232
}
3333

34-
impl<T: 'static + Copy> TypedAction<T> for StoreConst<T> {
34+
impl<T: 'static + Clone> TypedAction<T> for StoreConst<T> {
3535
fn bind<'x>(&self, cell: Rc<RefCell<&'x mut T>>) -> Action<'x> {
36-
let StoreConst(val) = *self;
37-
return Flag(Box::new(StoreConstAction { cell: cell, value: val }));
36+
let StoreConst(ref val) = *self;
37+
return Flag(Box::new(StoreConstAction { cell: cell, value: val.clone() }));
3838
}
3939
}
4040

41-
impl<T: 'static + Copy> TypedAction<Vec<T>> for PushConst<T> {
41+
impl<T: 'static + Clone> TypedAction<Vec<T>> for PushConst<T> {
4242
fn bind<'x>(&self, cell: Rc<RefCell<&'x mut Vec<T>>>) -> Action<'x> {
43-
let PushConst(val) = *self;
44-
return Flag(Box::new(PushConstAction { cell: cell, value: val }));
43+
let PushConst(ref val) = *self;
44+
return Flag(Box::new(PushConstAction { cell: cell, value: val.clone() }));
4545
}
4646
}
4747

@@ -69,18 +69,18 @@ impl<T: 'static + FromStr + Clone> TypedAction<Vec<T>> for Collect {
6969
}
7070
}
7171

72-
impl<'a, T: Copy> IFlagAction for StoreConstAction<'a, T> {
72+
impl<'a, T: Clone> IFlagAction for StoreConstAction<'a, T> {
7373
fn parse_flag(&self) -> ParseResult {
7474
let mut targ = self.cell.borrow_mut();
75-
**targ = self.value;
75+
**targ = self.value.clone();
7676
return Parsed;
7777
}
7878
}
7979

80-
impl<'a, T: Copy> IFlagAction for PushConstAction<'a, T> {
80+
impl<'a, T: Clone> IFlagAction for PushConstAction<'a, T> {
8181
fn parse_flag(&self) -> ParseResult {
8282
let mut targ = self.cell.borrow_mut();
83-
targ.push(self.value);
83+
targ.push(self.value.clone());
8484
return Parsed;
8585
}
8686
}

src/num.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,40 @@ pub struct DecrByAction<'a, T: 'a> {
1818
cell: Rc<RefCell<&'a mut T>>,
1919
}
2020

21-
impl<T: 'static + Add<Output = T> + Copy> TypedAction<T> for IncrBy<T> {
21+
impl<T: 'static + Add<Output = T> + Clone> TypedAction<T> for IncrBy<T> {
2222
fn bind<'x>(&self, cell: Rc<RefCell<&'x mut T>>) -> Action<'x> {
23-
let IncrBy(delta) = *self;
24-
return Flag(Box::new(IncrByAction { cell: cell, delta: delta }));
23+
let IncrBy(ref delta) = *self;
24+
return Flag(Box::new(IncrByAction { cell: cell, delta: delta.clone() }));
2525
}
2626
}
2727

28-
impl<T: 'static + Sub<Output = T> + Copy> TypedAction<T> for DecrBy<T> {
28+
impl<T: 'static + Sub<Output = T> + Clone> TypedAction<T> for DecrBy<T> {
2929
fn bind<'x>(&self, cell: Rc<RefCell<&'x mut T>>) -> Action<'x> {
30-
let DecrBy(delta) = *self;
31-
return Flag(Box::new(DecrByAction { cell: cell, delta: delta }));
30+
let DecrBy(ref delta) = *self;
31+
return Flag(Box::new(DecrByAction { cell: cell, delta: delta.clone() }));
3232
}
3333
}
3434

35-
impl<'a, T: Add<Output = T> + Copy> IFlagAction for IncrByAction<'a, T> {
35+
impl<'a, T: Add<Output = T> + Clone> IFlagAction for IncrByAction<'a, T> {
3636
fn parse_flag(&self) -> ParseResult {
37+
let oldval = {
38+
let targ = self.cell.borrow();
39+
targ.clone()
40+
};
3741
let mut targ = self.cell.borrow_mut();
38-
let oldval = **targ;
39-
**targ = oldval + self.delta;
42+
**targ = oldval + self.delta.clone();
4043
return Parsed;
4144
}
4245
}
4346

44-
impl<'a, T: Sub<Output = T> + Copy> IFlagAction for DecrByAction<'a, T> {
47+
impl<'a, T: Sub<Output = T> + Clone> IFlagAction for DecrByAction<'a, T> {
4548
fn parse_flag(&self) -> ParseResult {
49+
let oldval = {
50+
let targ = self.cell.borrow();
51+
targ.clone()
52+
};
4653
let mut targ = self.cell.borrow_mut();
47-
let oldval = **targ;
48-
**targ = oldval - self.delta;
54+
**targ = oldval - self.delta.clone();
4955
return Parsed;
5056
}
5157
}

src/test_parser.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::str::from_utf8;
21

32
use parser::ArgumentParser;
43

0 commit comments

Comments
 (0)