Interfacing with Rust

Ruby The Roobster rubytheroobster at yandex.com
Sat Oct 1 02:05:30 UTC 2022


On Friday, 30 September 2022 at 06:25:33 UTC, Imperatorn wrote:
> On Friday, 30 September 2022 at 00:18:42 UTC, Ruby The Roobster 
> wrote:
>> On Thursday, 29 September 2022 at 16:07:59 UTC, mw wrote:
>>> On Thursday, 29 September 2022 at 16:02:43 UTC, Ruby The 
>>> Roobster wrote:
>>>> Is there any way one can interface with Rust, such as with a 
>>>> struct, or a function?
>>>>
>>>> I know that rust has an extern keyword, but I can't get it 
>>>> to work.
>>>
>>> https://code.dlang.org/packages/rust_interop_d
>>>
>>>
>>> Read the notes on memory management:
>>>
>>> Only pass pointers as u64 as value type.
>>
>> This isn't the issue.  I can't interface anything, period.
>
> Show an example of exactly what you're trying to do. Maybe it's 
> some detail

lib.rs:

```rs
     #[derive(Debug)]
     struct Rectangle {
         width: u32,
         height: u32,
     }

     impl Rectangle {
         extern "cdecl" fn area(&self) -> u32 {
             self.width * self.height
         }

         extern "cdecl" fn can_hold(&self, other: &Rectangle) -> 
bool {
             self.width > other.width && self.height > other.height
         }
     }
```

main.d:

```d
     struct Rectangle
     {
         uint width;
         uint hight;
         extern(C++) uint area();
         extern(C++) bool can_hold(Rectangle rhs)
     }

     void main()
     {
         auto rect1 = Rectangle(1,1);
         auto rect2 = Rectangle(0,0);
         assert(rect1.area == 1 && rect2.area == 0 && 
rect1.canHold(rect2));
     }
```

lib.rs was built as a "staticlib" using rustc, and the command I 
used was:

ldc2 -m64 main.d lib.lib

The output:

```
main.obj : error LNK2019: unresolved external symbol "public: 
unsigned int __cdecl Rectangle::area(void)" 
(?area at Rectangle@@QEAAIXZ) referenced in function _Dmain
main.obj : error LNK2019: unresolved external symbol "public: 
bool __cdecl Rectangle::can_hold(struct Rectangle)" 
(?can_hold at Rectangle@@QEAA_NU1@@Z) referenced in function _Dmain
main.exe : fatal error LNK1120: 2 unresolved externals
Error: C:\Program Files (x86)\Microsoft Visual 
Studio\2022\BuildTools\VC\Tools\MSVC\14.33.31629\bin\HostX64\x64\link.exe failed with status: 1120
```

Also, is it normal for the .lib file to be 11 megabytes?


More information about the Digitalmars-d-learn mailing list