Skip to content

Commit 9964af9

Browse files
committed
Make ForeignPtr and Ptr coexist
1 parent 6e48bab commit 9964af9

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

src/Language/Rust/Inline/Context.hs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,6 @@ foreignPointers = do
319319
foreignPtrT <- [t|ForeignPtr|]
320320
pure $ Context ([rule], [rev foreignPtrT], [foreignPtr, constPtr, mutPtr])
321321
where
322-
rule (Ptr _ t _) context
323-
| First (Just (t', Nothing)) <- lookupRTypeInContext t context = pure ([t|ForeignPtr $t'|], Nothing)
324322
rule (Rptr _ _ t _) context
325323
| First (Just (t', Nothing)) <- lookupRTypeInContext t context = pure ([t|ForeignPtr $t'|], Nothing)
326324
rule (PathTy Nothing (Path False [PathSegment "ForeignPtr" (Just (AngleBracketed [] [t] [] _)) _] _) _) context
@@ -330,10 +328,6 @@ foreignPointers = do
330328
pure ([t|Maybe (ForeignPtr $t')|], pure . pure $ PathTy Nothing (Path False [PathSegment "ForeignPtr" (Just (AngleBracketed [] [t] [] ())) ()] ()) ())
331329
rule _ _ = mempty
332330

333-
rev foreignPtrT (AppT foreignPtr t) context
334-
| foreignPtr == foreignPtrT = do
335-
t' <- lookupHTypeInContext t context
336-
pure (Ptr Mutable <$> t' <*> pure ())
337331
rev _ _ _ = mempty
338332

339333
foreignPtr =
@@ -344,11 +338,7 @@ foreignPointers = do
344338

345339
constPtr =
346340
unlines
347-
[ "impl<T> MarshalInto<*const T> for *const T {"
348-
, " fn marshal(self) -> *const T { self }"
349-
, "}"
350-
, ""
351-
, "impl<'a, T> MarshalInto<&'a T> for &'a T {"
341+
[ "impl<'a, T> MarshalInto<&'a T> for &'a T {"
352342
, " fn marshal(self) -> &'a T { self }"
353343
, "}"
354344
]
@@ -378,10 +368,6 @@ foreignPointers = do
378368
, " }"
379369
, "}"
380370
, ""
381-
, "impl<T> MarshalInto<*mut T> for *mut T {"
382-
, " fn marshal(self) -> *mut T { self }"
383-
, "}"
384-
, ""
385371
, "impl<'a, T> MarshalInto<&'a mut T> for &'a mut T {"
386372
, " fn marshal(self) -> &'a mut T { self }"
387373
, "}"

tests/ForeignPtr.hs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ import Foreign.Ptr
1414
import Test.Hspec
1515

1616
extendContext foreignPointers
17+
extendContext pointers
1718
extendContext prelude
1819
extendContext basic
1920
setCrateModule
2021

2122
foreignPtrTypes :: Spec
2223
foreignPtrTypes = describe "ForeignPtr types" $ do
23-
it "Can marshal ForeignPtr arguments" $ do
24-
p <- mallocForeignPtr
25-
withForeignPtr p (`poke` 42)
26-
let read = [rust| u64 { unsafe { *$(p: *const u64) } } |]
27-
42 `shouldBe` read
28-
2924
it "Can marshal ForeignPtr arguments as references" $ do
3025
p <- mallocForeignPtr
3126
withForeignPtr p (`poke` 42)
@@ -46,14 +41,6 @@ foreignPtrTypes = describe "ForeignPtr types" $ do
4641
prev `shouldBe` 42
4742
withForeignPtr p peek >>= (`shouldBe` 43)
4843

49-
it "Can mutate ForeignPtr arguments" $ do
50-
p <- mallocForeignPtr
51-
[rustIO| () {
52-
unsafe { *$(p: *mut u64) = 42; }
53-
} |]
54-
val <- withForeignPtr p peek
55-
val `shouldBe` 42
56-
5744
it "Can marshal ForeignPtr returns" $ do
5845
let p = [rust| ForeignPtr<u64> { Box::new(42).into() }|]
5946
val <- withForeignPtr p peek
@@ -71,3 +58,16 @@ foreignPtrTypes = describe "ForeignPtr types" $ do
7158
Some(Box::new(42).into())
7259
} |]
7360
withForeignPtr (fromJust mp) peek >>= (`shouldBe` 42)
61+
62+
it "still has working pointers" $
63+
alloca $ \p -> do
64+
[rustIO| () {
65+
unsafe { *$(p: *mut u64) = 42; }
66+
} |]
67+
peek p >>= (`shouldBe` 42)
68+
69+
val <-
70+
[rustIO| u64 {
71+
unsafe { *$(p: *u64) }
72+
} |]
73+
val `shouldBe` 42

0 commit comments

Comments
 (0)