@@ -130,6 +130,108 @@ impl Comparator for Descend {
130130 }
131131}
132132
133+ /// Returns when the bytes are too large to be written to the occupied value.
134+ #[ derive( Debug , Default , Clone , Copy ) ]
135+ pub struct TooLarge {
136+ remaining : usize ,
137+ write : usize ,
138+ }
139+
140+ impl core:: fmt:: Display for TooLarge {
141+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
142+ write ! (
143+ f,
144+ "OccupiedValue does not have enough space (remaining {}, want {})" ,
145+ self . remaining, self . write
146+ )
147+ }
148+ }
149+
150+ #[ cfg( feature = "std" ) ]
151+ impl std:: error:: Error for TooLarge { }
152+
153+ /// An occupied value in the skiplist.
154+ #[ must_use = "occupied value must be fully filled with bytes." ]
155+ #[ derive( Debug ) ]
156+ pub struct OccupiedValue < ' a > {
157+ value : & ' a mut [ u8 ] ,
158+ len : usize ,
159+ cap : usize ,
160+ }
161+
162+ impl < ' a > OccupiedValue < ' a > {
163+ /// Write bytes to the occupied value.
164+ pub fn write ( & mut self , bytes : & [ u8 ] ) -> Result < ( ) , TooLarge > {
165+ let len = bytes. len ( ) ;
166+ let remaining = self . cap - self . len ;
167+ if len > remaining {
168+ return Err ( TooLarge {
169+ remaining,
170+ write : len,
171+ } ) ;
172+ }
173+
174+ self . value [ self . len ..self . len + len] . copy_from_slice ( bytes) ;
175+ self . len += len;
176+ Ok ( ( ) )
177+ }
178+
179+ /// Returns the capacity of the occupied value.
180+ #[ inline]
181+ pub const fn capacity ( & self ) -> usize {
182+ self . cap
183+ }
184+
185+ /// Returns the length of the occupied value.
186+ #[ inline]
187+ pub const fn len ( & self ) -> usize {
188+ self . len
189+ }
190+
191+ /// Returns `true` if the occupied value is empty.
192+ #[ inline]
193+ pub const fn is_empty ( & self ) -> bool {
194+ self . len == 0
195+ }
196+
197+ /// Returns the remaining space of the occupied value.
198+ #[ inline]
199+ pub const fn remaining ( & self ) -> usize {
200+ self . cap - self . len
201+ }
202+
203+ #[ inline]
204+ fn new ( cap : usize , value : & ' a mut [ u8 ] ) -> Self {
205+ Self { value, len : 0 , cap }
206+ }
207+ }
208+
209+ impl < ' a > core:: ops:: Deref for OccupiedValue < ' a > {
210+ type Target = [ u8 ] ;
211+
212+ fn deref ( & self ) -> & Self :: Target {
213+ & self . value [ ..self . len ]
214+ }
215+ }
216+
217+ impl < ' a > core:: ops:: DerefMut for OccupiedValue < ' a > {
218+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
219+ & mut self . value [ ..self . len ]
220+ }
221+ }
222+
223+ impl < ' a > Drop for OccupiedValue < ' a > {
224+ fn drop ( & mut self ) {
225+ assert_eq ! (
226+ self . len,
227+ self . cap,
228+ "OccupiedValue was not fully filled with bytes, capacity is {}, remaining is {}" ,
229+ self . cap,
230+ self . cap - self . len
231+ ) ;
232+ }
233+ }
234+
133235/// A trait for extra information that can be stored with entry in the skiplist.
134236pub trait Trailer : Copy {
135237 /// Returns the version of the trailer.
0 commit comments