Skip to content

Commit 9e05640

Browse files
committed
code simplifications
1 parent 56fe426 commit 9e05640

152 files changed

Lines changed: 591 additions & 818 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2628,7 +2628,7 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
26282628
auto *sub_expr = expr->getSubExpr();
26292629
if (type->isVoidType()) {
26302630
if (!sub_expr->HasSideEffects(ctx_)) {
2631-
EmitFresh("()");
2631+
EmitFresh(isVoid() ? "" : "()");
26322632
return false;
26332633
}
26342634
StrCat(token::kRef);

‎libcc2rs/src/field.rs‎

Lines changed: 50 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use std::any::{Any, TypeId};
2121
use std::cell::{Cell, Ref, RefCell, RefMut};
2222
use std::marker::PhantomData;
23+
use std::ops::DerefMut;
2324
use std::rc::{Rc, Weak};
2425

2526
use crate::rc::{
@@ -645,6 +646,19 @@ pub trait Place: Clone {
645646
type Target;
646647
fn with<R>(&self, f: impl FnOnce(&Self::Target) -> R) -> R;
647648
fn with_mut<R>(&self, f: impl FnOnce(&mut Self::Target) -> R) -> R;
649+
650+
#[inline]
651+
fn read(&self) -> Self::Target
652+
where
653+
Self::Target: Clone,
654+
{
655+
self.with(Clone::clone)
656+
}
657+
658+
#[inline]
659+
fn write(&self, value: Self::Target) {
660+
self.with_mut(|v| *v = value)
661+
}
648662
}
649663

650664
impl<T: ByteRepr> Place for Ptr<T> {
@@ -677,40 +691,15 @@ impl<B: Place, T> Clone for FieldPlace<'_, B, T> {
677691

678692
impl<B: Place, T> Copy for FieldPlace<'_, B, T> {}
679693

680-
impl<B: Place, T> FieldPlace<'_, B, T> {
681-
#[inline]
682-
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
683-
self.base.with(|s| f((self.get)(s)))
684-
}
685-
686-
#[inline]
687-
pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
688-
self.base.with_mut(|s| f((self.get_mut)(s)))
689-
}
690-
691-
#[inline]
692-
pub fn read(&self) -> T
693-
where
694-
T: Clone,
695-
{
696-
self.with(T::clone)
697-
}
698-
699-
#[inline]
700-
pub fn write(&self, value: T) {
701-
self.with_mut(|v| *v = value)
702-
}
703-
}
704-
705694
impl<B: Place, T> Place for FieldPlace<'_, B, T> {
706695
type Target = T;
707696
#[inline]
708697
fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
709-
FieldPlace::with(self, f)
698+
self.base.with(|s| f((self.get)(s)))
710699
}
711700
#[inline]
712701
fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
713-
FieldPlace::with_mut(self, f)
702+
self.base.with_mut(|s| f((self.get_mut)(s)))
714703
}
715704
}
716705

@@ -747,44 +736,34 @@ impl<T> Copy for ElemPlace<'_, T> {}
747736

748737
impl<T: ByteRepr> ElemPlace<'_, T> {
749738
#[inline]
750-
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
751-
self.base.with_at(self.idx, f)
739+
pub fn upgrade(&self) -> StrongPtr<T> {
740+
self.base.offset(self.idx).upgrade()
752741
}
742+
}
753743

744+
impl<T: ByteRepr> Place for ElemPlace<'_, T> {
745+
type Target = T;
754746
#[inline]
755-
pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
747+
fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
748+
self.base.with_at(self.idx, f)
749+
}
750+
#[inline]
751+
fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
756752
self.base.with_mut_at(self.idx, f)
757753
}
758-
754+
// Elements are read and written without the extra call of with, which
755+
// keeps them inlined in hot loops.
759756
#[inline]
760-
pub fn read(&self) -> T
757+
fn read(&self) -> T
761758
where
762759
T: Clone,
763760
{
764761
self.base.read_at(self.idx)
765762
}
766-
767763
#[inline]
768-
pub fn write(&self, value: T) {
764+
fn write(&self, value: T) {
769765
self.base.write_at(self.idx, value)
770766
}
771-
772-
#[inline]
773-
pub fn upgrade(&self) -> StrongPtr<T> {
774-
self.base.offset(self.idx).upgrade()
775-
}
776-
}
777-
778-
impl<T: ByteRepr> Place for ElemPlace<'_, T> {
779-
type Target = T;
780-
#[inline]
781-
fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
782-
ElemPlace::with(self, f)
783-
}
784-
#[inline]
785-
fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
786-
ElemPlace::with_mut(self, f)
787-
}
788767
}
789768

790769
// Element `idx` of the sequence that pointer `base` points into, e.g.,
@@ -801,37 +780,6 @@ macro_rules! elem {
801780
};
802781
}
803782

804-
// An array or vector, whose elements are indexed by IndexPlace.
805-
pub trait Seq {
806-
type Elem;
807-
fn elems(&self) -> &[Self::Elem];
808-
fn elems_mut(&mut self) -> &mut [Self::Elem];
809-
}
810-
811-
impl<T> Seq for Box<[T]> {
812-
type Elem = T;
813-
#[inline]
814-
fn elems(&self) -> &[T] {
815-
self
816-
}
817-
#[inline]
818-
fn elems_mut(&mut self) -> &mut [T] {
819-
self
820-
}
821-
}
822-
823-
impl<T> Seq for Vec<T> {
824-
type Elem = T;
825-
#[inline]
826-
fn elems(&self) -> &[T] {
827-
self
828-
}
829-
#[inline]
830-
fn elems_mut(&mut self) -> &mut [T] {
831-
self
832-
}
833-
}
834-
835783
// Element `idx` of the array or vector in place `base`, e.g., a row of an
836784
// array field of a struct reached through a pointer. Written by
837785
// `index!(place, i)`. It is a struct expression, like elem!.
@@ -849,53 +797,26 @@ impl<B> Clone for IndexPlace<'_, B> {
849797

850798
impl<B> Copy for IndexPlace<'_, B> {}
851799

852-
impl<B: Place> IndexPlace<'_, B>
853-
where
854-
B::Target: Seq,
855-
{
856-
#[inline]
857-
pub fn with<R>(&self, f: impl FnOnce(&<B::Target as Seq>::Elem) -> R) -> R {
858-
self.base.with(|c| f(&c.elems()[self.idx]))
859-
}
860-
861-
#[inline]
862-
pub fn with_mut<R>(&self, f: impl FnOnce(&mut <B::Target as Seq>::Elem) -> R) -> R {
863-
self.base.with_mut(|c| f(&mut c.elems_mut()[self.idx]))
864-
}
865-
866-
#[inline]
867-
pub fn read(&self) -> <B::Target as Seq>::Elem
868-
where
869-
<B::Target as Seq>::Elem: Clone,
870-
{
871-
self.with(Clone::clone)
872-
}
873-
874-
#[inline]
875-
pub fn write(&self, value: <B::Target as Seq>::Elem) {
876-
self.with_mut(|v| *v = value)
877-
}
878-
}
879-
880800
impl<T: ByteRepr> IndexPlace<'_, Ptr<Vec<T>>> {
881801
#[inline]
882802
pub fn upgrade(&self) -> StrongPtr<T> {
883803
self.base.decay().offset(self.idx).upgrade()
884804
}
885805
}
886806

887-
impl<B: Place> Place for IndexPlace<'_, B>
807+
// The array or vector is a slice of its elements.
808+
impl<B: Place, E> Place for IndexPlace<'_, B>
888809
where
889-
B::Target: Seq,
810+
B::Target: DerefMut<Target = [E]>,
890811
{
891-
type Target = <B::Target as Seq>::Elem;
812+
type Target = E;
892813
#[inline]
893-
fn with<R>(&self, f: impl FnOnce(&Self::Target) -> R) -> R {
894-
IndexPlace::with(self, f)
814+
fn with<R>(&self, f: impl FnOnce(&E) -> R) -> R {
815+
self.base.with(|c| f(&c[self.idx]))
895816
}
896817
#[inline]
897-
fn with_mut<R>(&self, f: impl FnOnce(&mut Self::Target) -> R) -> R {
898-
IndexPlace::with_mut(self, f)
818+
fn with_mut<R>(&self, f: impl FnOnce(&mut E) -> R) -> R {
819+
self.base.with_mut(|c| f(&mut c[self.idx]))
899820
}
900821
}
901822

@@ -942,36 +863,6 @@ impl<S: Record, T: ByteRepr> ArrayElem<'_, S, T> {
942863
.offset(self.idx)
943864
}
944865

945-
#[inline]
946-
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
947-
if let PtrKind::Reinterpreted(_) = self.base.kind {
948-
return self.ptr().with(f);
949-
}
950-
self.base.with(|s| f(&(self.get)(s)[self.idx as usize]))
951-
}
952-
953-
#[inline]
954-
pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
955-
if let PtrKind::Reinterpreted(_) = self.base.kind {
956-
return self.ptr().with_mut(f);
957-
}
958-
self.base
959-
.with_mut(|s| f(&mut (self.get_mut)(s)[self.idx as usize]))
960-
}
961-
962-
#[inline]
963-
pub fn read(&self) -> T
964-
where
965-
T: Clone,
966-
{
967-
self.with(T::clone)
968-
}
969-
970-
#[inline]
971-
pub fn write(&self, value: T) {
972-
self.with_mut(|v| *v = value)
973-
}
974-
975866
#[inline]
976867
pub fn upgrade(&self) -> StrongPtr<T> {
977868
self.ptr().upgrade()
@@ -982,11 +873,18 @@ impl<S: Record, T: ByteRepr> Place for ArrayElem<'_, S, T> {
982873
type Target = T;
983874
#[inline]
984875
fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
985-
ArrayElem::with(self, f)
876+
if let PtrKind::Reinterpreted(_) = self.base.kind {
877+
return self.ptr().with(f);
878+
}
879+
self.base.with(|s| f(&(self.get)(s)[self.idx as usize]))
986880
}
987881
#[inline]
988882
fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
989-
ArrayElem::with_mut(self, f)
883+
if let PtrKind::Reinterpreted(_) = self.base.kind {
884+
return self.ptr().with_mut(f);
885+
}
886+
self.base
887+
.with_mut(|s| f(&mut (self.get_mut)(s)[self.idx as usize]))
990888
}
991889
}
992890

@@ -1026,29 +924,6 @@ impl<S: Record, T> Clone for VecElem<'_, S, T> {
1026924
impl<S: Record, T> Copy for VecElem<'_, S, T> {}
1027925

1028926
impl<S: Record, T: ByteRepr> VecElem<'_, S, T> {
1029-
#[inline]
1030-
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
1031-
self.base.with(|s| f(&(self.get)(s)[self.idx]))
1032-
}
1033-
1034-
#[inline]
1035-
pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
1036-
self.base.with_mut(|s| f(&mut (self.get_mut)(s)[self.idx]))
1037-
}
1038-
1039-
#[inline]
1040-
pub fn read(&self) -> T
1041-
where
1042-
T: Clone,
1043-
{
1044-
self.with(T::clone)
1045-
}
1046-
1047-
#[inline]
1048-
pub fn write(&self, value: T) {
1049-
self.with_mut(|v| *v = value)
1050-
}
1051-
1052927
#[inline]
1053928
pub fn upgrade(&self) -> StrongPtr<T> {
1054929
let elems: Ptr<T> = self
@@ -1063,11 +938,11 @@ impl<S: Record, T: ByteRepr> Place for VecElem<'_, S, T> {
1063938
type Target = T;
1064939
#[inline]
1065940
fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
1066-
VecElem::with(self, f)
941+
self.base.with(|s| f(&(self.get)(s)[self.idx]))
1067942
}
1068943
#[inline]
1069944
fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
1070-
VecElem::with_mut(self, f)
945+
self.base.with_mut(|s| f(&mut (self.get_mut)(s)[self.idx]))
1071946
}
1072947
}
1073948

@@ -1273,7 +1148,7 @@ pub(crate) fn upgrade(root: &Weak<dyn Root>) -> Rc<dyn Root> {
12731148

12741149
#[cfg(test)]
12751150
mod tests {
1276-
use crate::{AsPointer, ByteRepr, Decay, Ptr, Record, Value};
1151+
use crate::{AsPointer, ByteRepr, Decay, Place, Ptr, Record, Value};
12771152
use std::cell::RefCell;
12781153
use std::rc::Rc;
12791154

@@ -1550,10 +1425,7 @@ mod tests {
15501425
name.offset(1)
15511426
.with_slice_mut(2, |b| b.copy_from_slice(b"xy"));
15521427
assert_eq!(&s.borrow().name[..4], b"axy\0");
1553-
assert_eq!(name.slice_until(&name.offset(3)), b"axy".to_vec());
15541428
let nums: Ptr<i32> = field_ptr!(s, nums).decay();
1555-
nums.sort(3);
1556-
assert_eq!(s.borrow().nums, vec![1, 2, 3]);
15571429
assert_eq!(nums.len(), 3);
15581430
}
15591431

‎libcc2rs/src/fn_ptr.rs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ impl<T: FnSig> FnPtr<T> {
179179
// type. Casting the result back to a type it already had recovers that
180180
// exact function, calling it directly rather than through an adaptor.
181181
pub fn cast<U: FnSig>(&self) -> FnPtr<U> {
182-
assert!(!self.is_null(), "ub: null fn pointer cast");
182+
if self.is_null() {
183+
return FnPtr::null();
184+
}
183185

184186
// The current function, if it already has type U.
185187
let current_as_u = self.current.as_ref().and_then(|current| {
@@ -567,9 +569,10 @@ mod tests {
567569
}
568570

569571
#[test]
570-
#[should_panic(expected = "ub: null fn pointer cast")]
571-
fn cast_null_panics() {
572-
FnPtr::<fn(i32) -> i32>::null().cast::<fn(u32) -> u32>();
572+
fn cast_null_is_null() {
573+
let f = FnPtr::<fn(i32) -> i32>::null().cast::<fn(u32) -> u32>();
574+
assert!(f.is_null());
575+
assert!(f.cast::<fn(i32) -> i32>().is_null());
573576
}
574577

575578
#[test]

0 commit comments

Comments
 (0)