Skip to content

Commit c8c1103

Browse files
committed
refactor(cell): simplify the usage of CellSliceParts
1 parent 9742495 commit c8c1103

File tree

11 files changed

+78
-55
lines changed

11 files changed

+78
-55
lines changed

src/abi/contract.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use super::error::AbiError;
1111
use super::{AbiHeaderType, AbiType, AbiValue, AbiVersion, NamedAbiType, NamedAbiValue};
1212
use crate::abi::value::ser::AbiSerializer;
1313
use crate::abi::AbiHeader;
14-
use crate::cell::{
15-
Cell, CellBuilder, CellFamily, CellSlice, CellSliceRange, DynCell, HashBytes, Size, Store,
16-
};
14+
use crate::cell::{Cell, CellBuilder, CellFamily, CellSlice, DynCell, HashBytes, Size, Store};
1715
use crate::dict::RawDict;
1816
use crate::models::{
1917
ExtInMsgInfo, IntAddr, MsgInfo, OwnedMessage, OwnedRelaxedMessage, RelaxedIntMsgInfo,
@@ -530,8 +528,7 @@ impl Function {
530528
state_init: Option<&StateInit>,
531529
) -> Result<Box<OwnedRelaxedMessage>> {
532530
let body = self.encode_internal_input(tokens)?;
533-
let cell = body.build()?;
534-
let range = CellSliceRange::full(cell.as_ref());
531+
let body = body.build()?;
535532

536533
Ok(Box::new(OwnedRelaxedMessage {
537534
info: RelaxedMsgInfo::Int(RelaxedIntMsgInfo {
@@ -540,7 +537,7 @@ impl Function {
540537
value: value.into(),
541538
..Default::default()
542539
}),
543-
body: (cell, range),
540+
body: body.into(),
544541
init: state_init.cloned(),
545542
layout: None,
546543
}))
@@ -647,13 +644,12 @@ impl<'a> ExternalInput<'_, 'a> {
647644
address: &StdAddr,
648645
) -> Result<(u32, OwnedMessage)> {
649646
let (expire_at, body) = ok!(self.build_input_without_signature());
650-
let range = CellSliceRange::full(body.as_ref());
651647
Ok((expire_at, OwnedMessage {
652648
info: MsgInfo::ExtIn(ExtInMsgInfo {
653649
dst: IntAddr::Std(address.clone()),
654650
..Default::default()
655651
}),
656-
body: (body, range),
652+
body: body.into(),
657653
init: None,
658654
layout: None,
659655
}))
@@ -1126,13 +1122,12 @@ impl UnsignedExternalMessage {
11261122
/// Returns an external message with filled signature.
11271123
pub fn fill_signature(&self, signature: Option<&[u8; 64]>) -> Result<OwnedMessage> {
11281124
let body = self.body.fill_signature(signature)?;
1129-
let range = CellSliceRange::full(body.as_ref());
11301125
Ok(OwnedMessage {
11311126
info: MsgInfo::ExtIn(ExtInMsgInfo {
11321127
dst: IntAddr::Std(self.dst.clone()),
11331128
..Default::default()
11341129
}),
1135-
body: (body, range),
1130+
body: body.into(),
11361131
init: self.init.clone(),
11371132
layout: None,
11381133
})
@@ -1141,13 +1136,12 @@ impl UnsignedExternalMessage {
11411136
/// Converts an unsigned message into an external message with filled signature.
11421137
fn into_signed(self, signature: Option<&[u8; 64]>) -> Result<OwnedMessage> {
11431138
let body = self.body.fill_signature(signature)?;
1144-
let range = CellSliceRange::full(body.as_ref());
11451139
Ok(OwnedMessage {
11461140
info: MsgInfo::ExtIn(ExtInMsgInfo {
11471141
dst: IntAddr::Std(self.dst),
11481142
..Default::default()
11491143
}),
1150-
body: (body, range),
1144+
body: body.into(),
11511145
init: self.init,
11521146
layout: None,
11531147
})

src/cell/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ impl<'a> arbitrary::Arbitrary<'a> for Cell {
413413
}
414414
}
415415

416+
impl From<Cell> for CellSliceParts {
417+
#[inline]
418+
fn from(value: Cell) -> Self {
419+
(CellSliceRange::full(&value), value)
420+
}
421+
}
422+
416423
/// An iterator through child nodes.
417424
#[must_use = "iterators are lazy and do nothing unless consumed"]
418425
pub struct RefsIter<'a> {

src/cell/slice.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,12 @@ impl ExactSize for Cell {
221221
}
222222

223223
/// Owned cell slice parts alias.
224-
pub type CellSliceParts = (Cell, CellSliceRange);
224+
pub type CellSliceParts = (CellSliceRange, Cell);
225225

226226
impl ExactSize for CellSliceParts {
227227
#[inline]
228228
fn exact_size(&self) -> Size {
229-
self.1.size()
229+
self.0.size()
230230
}
231231
}
232232

@@ -251,7 +251,12 @@ impl CellSliceRange {
251251
}
252252

253253
/// Returns a full range for the specified cell.
254-
pub fn full(cell: &DynCell) -> Self {
254+
#[inline]
255+
pub fn full<T>(cell: &T) -> Self
256+
where
257+
T: AsRef<DynCell> + ?Sized,
258+
{
259+
let cell = cell.as_ref();
255260
Self {
256261
bits_start: 0,
257262
bits_end: cell.bit_len(),
@@ -473,7 +478,11 @@ impl CellSliceRange {
473478

474479
/// Returns whether this range has the same size as the cell.
475480
#[inline]
476-
pub fn is_full(&self, cell: &DynCell) -> bool {
481+
pub fn is_full<T>(&self, cell: &T) -> bool
482+
where
483+
T: AsRef<DynCell> + ?Sized,
484+
{
485+
let cell = cell.as_ref();
477486
self.bits_start == 0
478487
&& self.refs_start == 0
479488
&& self.bits_end == cell.bit_len()
@@ -532,6 +541,26 @@ impl<'a> CellSlice<'a> {
532541
}
533542
}
534543

544+
/// Alias for [`CellSliceRange::apply`].
545+
///
546+
/// NOTE: the resulting range will be truncated to cell bounds.
547+
#[inline]
548+
pub fn apply((range, cell): &'a CellSliceParts) -> Result<Self, Error> {
549+
range.apply(cell)
550+
}
551+
552+
/// Alias for [`CellSliceRange::apply_allow_exotic`].
553+
///
554+
/// # Safety
555+
///
556+
/// The following must be true:
557+
/// - cell is not pruned
558+
/// - range is in cell bounds
559+
#[inline]
560+
pub fn apply_allow_exotic((range, cell): &'a CellSliceParts) -> Self {
561+
range.apply_allow_exotic(cell)
562+
}
563+
535564
/// Returns an underlying range indices.
536565
#[inline]
537566
pub const fn range(&self) -> CellSliceRange {

src/dict/aug.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ where
488488
for<'a> V: Load<'a> + 'static,
489489
{
490490
match ok!(self.remove_raw_ext(key, Cell::empty_context())) {
491-
Some((cell, range)) => {
492-
let mut slice = ok!(range.apply(&cell));
491+
Some(parts) => {
492+
let mut slice = ok!(CellSlice::apply(&parts));
493493
let extra = ok!(A::load_from(&mut slice));
494494
let value = ok!(V::load_from(&mut slice));
495495
Ok(Some((extra, value)))
@@ -543,7 +543,7 @@ where
543543
&mut self,
544544
key: &K,
545545
context: &dyn CellContext,
546-
) -> Result<Option<(Cell, CellSliceRange)>, Error> {
546+
) -> Result<Option<CellSliceParts>, Error> {
547547
let mut key_builder = CellBuilder::new();
548548
ok!(key.store_into(&mut key_builder, Cell::empty_context()));
549549
let res = ok!(aug_dict_remove_owned(

src/dict/ops/find.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn dict_find_owned(
109109
let original_key = ok!(original_key_range.apply(key.cell()));
110110
ok!(result_key.store_slice_data(original_key));
111111

112-
return Ok(Some((result_key, (cell, value_range))));
112+
return Ok(Some((result_key, (value_range, cell))));
113113
}
114114
}
115115

@@ -209,7 +209,7 @@ pub fn dict_find_owned(
209209
None => root,
210210
};
211211

212-
Ok(Some((result_key, (cell, value_range))))
212+
Ok(Some((result_key, (value_range, cell))))
213213
}
214214

215215
/// Finds the specified dict bound and returns a key and a value corresponding to the key.
@@ -324,9 +324,9 @@ pub fn dict_find_bound_owned(
324324
Some(cell) => ok!(context.load_cell(cell, LoadMode::Resolve)),
325325
None => return Err(Error::CellUnderflow),
326326
};
327-
(cell, range)
327+
(range, cell)
328328
}
329-
None => (root, range),
329+
None => (range, root),
330330
};
331331

332332
// Return the last slice as data

src/dict/ops/get.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ pub fn dict_get_owned(
113113
Some(cell) => ok!(context.load_cell(cell, LoadMode::Resolve)),
114114
None => return Err(Error::CellUnderflow),
115115
};
116-
(cell, data.range())
116+
(data.range(), cell)
117117
}
118118
None => {
119119
let range = data.range();
120-
(root, range)
120+
(range, root)
121121
}
122122
})
123123
} else {

src/dict/ops/insert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ pub fn dict_insert_owned(
289289
if !mode.can_replace() {
290290
// TODO: change mode to `LoadMode::Noop` if copy-on-write for libraries is not ok.
291291
let value = (
292+
remaining_data.range(),
292293
ok!(last(&stack, context, LoadMode::Resolve))
293294
.unwrap_or_else(|| root.clone()),
294-
remaining_data.range(),
295295
);
296296
// TODO: what is the desired behavior for root as a library?
297297
return Ok((false, Some(value)));
@@ -364,7 +364,7 @@ pub fn dict_insert_owned(
364364
// TODO: change mode to `LoadMode::Noop` if copy-on-write for libraries is not ok
365365
let last = ok!(last(&stack, context, LoadMode::Resolve));
366366
leaf = ok!(rebuild_dict_from_stack(stack, leaf, context));
367-
Some((last.unwrap_or(root), range))
367+
Some((range, last.unwrap_or(root)))
368368
}
369369
None => {
370370
leaf = ok!(rebuild_dict_from_stack(stack, leaf, context));

src/dict/ops/remove.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn dict_remove_owned(
3939
value
4040
};
4141

42-
Ok(Some((value, removed)))
42+
Ok(Some((removed, value)))
4343
}
4444

4545
/// Removes the value associated with key in aug dictionary.
@@ -79,7 +79,7 @@ pub fn aug_dict_remove_owned(
7979
value
8080
};
8181

82-
Ok(Some((value, removed)))
82+
Ok(Some((removed, value)))
8383
}
8484

8585
fn dict_find_value_to_remove<'a, 'c: 'a>(
@@ -247,10 +247,10 @@ pub fn dict_remove_bound_owned(
247247
let leaf = ok!(builder.build_ext(context));
248248

249249
// Return the new cell and the removed one
250-
(leaf, (value, removed))
250+
(leaf, (removed, value))
251251
} else {
252252
*dict = None;
253-
return Ok(Some((key, (root, removed))));
253+
return Ok(Some((key, (removed, root))));
254254
};
255255

256256
*dict = Some(ok!(rebuild_dict_from_stack(stack, leaf, context)));

src/dict/raw.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl<'a> RawIter<'a> {
799799
}
800800
},
801801
};
802-
Ok((key, (parent, slice.range())))
802+
Ok((key, (slice.range(), parent)))
803803
}
804804
Err(e) => Err(e),
805805
})
@@ -1278,7 +1278,7 @@ impl Iterator for RawOwnedValues<'_> {
12781278
}
12791279
},
12801280
};
1281-
Ok((parent, slice.range()))
1281+
Ok((slice.range(), parent))
12821282
}
12831283
Err(e) => Err(e),
12841284
})
@@ -1687,7 +1687,7 @@ mod tests {
16871687
let value = dict.get(key.as_slice()?)?.unwrap();
16881688

16891689
{
1690-
let (cell, range) = dict.get_owned(key.as_slice()?)?.unwrap();
1690+
let (range, cell) = dict.get_owned(key.as_slice()?)?.unwrap();
16911691
assert_eq!(range.apply(&cell).unwrap(), value);
16921692
}
16931693

@@ -1730,7 +1730,7 @@ mod tests {
17301730

17311731
let mut last = None;
17321732
for (i, entry) in dict.iter_owned().enumerate() {
1733-
let (key, (cell, range)) = entry?;
1733+
let (key, (range, cell)) = entry?;
17341734

17351735
{
17361736
let mut slice = range.apply(&cell).unwrap();
@@ -1751,7 +1751,7 @@ mod tests {
17511751
let mut values_owned = dict.values_owned();
17521752
for (value_ref, value_owned) in (&mut values_ref).zip(&mut values_owned) {
17531753
let value_ref = value_ref.unwrap();
1754-
let (cell, range) = value_owned.unwrap();
1754+
let (range, cell) = value_owned.unwrap();
17551755
let value_owned = range.apply(&cell).unwrap();
17561756
assert_eq!(
17571757
value_ref.lex_cmp(&value_owned),

src/dict/typed.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ where
307307
for<'a> V: Load<'a> + 'static,
308308
{
309309
match ok!(self.remove_raw_ext(key, Cell::empty_context())) {
310-
Some((cell, range)) => {
311-
let mut slice = ok!(range.apply(&cell));
310+
Some(parts) => {
311+
let mut slice = ok!(CellSlice::apply(&parts));
312312
Ok(Some(ok!(V::load_from(&mut slice))))
313313
}
314314
None => Ok(None),
@@ -601,7 +601,7 @@ where
601601
for<'a> V: Load<'a>,
602602
{
603603
let context = Cell::empty_context();
604-
let Some((key, (cell, range))) = ({
604+
let Some((key, parts)) = ({
605605
let mut builder = CellBuilder::new();
606606
ok!(key.store_into(&mut builder, context));
607607
// TODO: add `dict_find` with non-owned return type
@@ -617,7 +617,7 @@ where
617617
}) else {
618618
return Ok(None);
619619
};
620-
let value = &mut ok!(range.apply(&cell));
620+
let value = &mut ok!(CellSlice::apply(&parts));
621621

622622
match K::from_raw_data(key.raw_data()) {
623623
Some(key) => Ok(Some((key, ok!(V::load_from(value))))),
@@ -1272,8 +1272,8 @@ mod tests {
12721272
dict.set(i, i < 0).unwrap();
12731273
}
12741274

1275-
let parse_removed = |(i, (cell, range)): (i32, CellSliceParts)| {
1276-
let mut value = range.apply(&cell)?;
1275+
let parse_removed = |(i, parts): (i32, CellSliceParts)| {
1276+
let mut value = CellSlice::apply(&parts)?;
12771277
let value = bool::load_from(&mut value)?;
12781278
Ok::<_, Error>((i, value))
12791279
};

0 commit comments

Comments
 (0)