24
24
//! If you want to discard uncommitted changes, simply call [`Data::restore`] first.
25
25
26
26
use std:: ops:: Range ;
27
- use std:: rc:: Rc ;
28
27
29
28
use crate :: error:: Error ;
30
29
31
30
/// Data that should replace a particular range of the original.
32
31
#[ derive( Clone ) ]
33
- struct Span {
32
+ struct Span < ' orig > {
34
33
/// Span of the parent data to be replaced, inclusive of the start, exclusive of the end.
35
34
range : Range < usize > ,
36
35
/// New data to insert at the `start` position of the `original` data.
37
- data : Rc < [ u8 ] > ,
36
+ data : & ' orig [ u8 ] ,
38
37
/// Whether this data is committed or provisional.
39
38
committed : bool ,
40
39
}
41
40
42
- impl std:: fmt:: Debug for Span {
41
+ impl < ' orig > std:: fmt:: Debug for Span < ' orig > {
43
42
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
44
43
let state = if self . is_insert ( ) {
45
44
"inserted"
@@ -61,11 +60,11 @@ impl std::fmt::Debug for Span {
61
60
}
62
61
}
63
62
64
- impl Span {
65
- fn new ( range : Range < usize > , data : & [ u8 ] ) -> Self {
63
+ impl < ' orig > Span < ' orig > {
64
+ fn new ( range : Range < usize > , data : & ' orig [ u8 ] ) -> Self {
66
65
Self {
67
66
range,
68
- data : data . into ( ) ,
67
+ data,
69
68
committed : false ,
70
69
}
71
70
}
@@ -79,7 +78,7 @@ impl Span {
79
78
}
80
79
}
81
80
82
- impl PartialEq for Span {
81
+ impl < ' orig > PartialEq for Span < ' orig > {
83
82
/// Returns `true` if and only if this `Span` and `other` have the same range and data,
84
83
/// regardless of `committed` status.
85
84
fn eq ( & self , other : & Self ) -> bool {
@@ -89,20 +88,20 @@ impl PartialEq for Span {
89
88
90
89
/// A container that allows easily replacing chunks of its data.
91
90
#[ derive( Debug , Clone , Default ) ]
92
- pub struct Data {
91
+ pub struct Data < ' orig > {
93
92
/// Original data.
94
- original : Vec < u8 > ,
93
+ original : & ' orig [ u8 ] ,
95
94
/// [`Span`]s covering the full range of the original data.
96
95
/// Important: it's expected that the underlying implementation maintains this in order,
97
96
/// sorted ascending by start position.
98
- parts : Vec < Span > ,
97
+ parts : Vec < Span < ' orig > > ,
99
98
}
100
99
101
- impl Data {
100
+ impl < ' orig > Data < ' orig > {
102
101
/// Create a new data container from a slice of bytes
103
- pub fn new ( data : & [ u8 ] ) -> Self {
102
+ pub fn new ( data : & ' orig [ u8 ] ) -> Self {
104
103
Data {
105
- original : data. into ( ) ,
104
+ original : data. as_ref ( ) ,
106
105
parts : vec ! [ ] ,
107
106
}
108
107
}
@@ -126,7 +125,7 @@ impl Data {
126
125
if span. range . start > prev_end {
127
126
acc. extend_from_slice ( & self . original [ prev_end..span. range . start ] ) ;
128
127
}
129
- acc. extend_from_slice ( & span. data ) ;
128
+ acc. extend_from_slice ( span. data ) ;
130
129
prev_end = span. range . end ;
131
130
acc
132
131
} ) ;
@@ -143,7 +142,7 @@ impl Data {
143
142
/// this method will return an error.
144
143
/// It will also return an error if the beginning of the range comes before its end,
145
144
/// or if the range is outside that of the original data.
146
- pub fn replace_range ( & mut self , range : Range < usize > , data : & [ u8 ] ) -> Result < ( ) , Error > {
145
+ pub fn replace_range ( & mut self , range : Range < usize > , data : & ' orig [ u8 ] ) -> Result < ( ) , Error > {
147
146
if range. start > range. end {
148
147
return Err ( Error :: InvalidRange ( range) ) ;
149
148
}
0 commit comments