• RustDay06------Exercise[91-100]


    91.将指针还原成指定类型

    因为指针不知道里面具体有什么,所以一般约定打上unsafe

    申明开发者自己对该部分可用性负责,且在调试的时候也能起强调作用

    1. // tests6.rs
    2. //
    3. // In this example we take a shallow dive into the Rust standard library's
    4. // unsafe functions. Fix all the question marks and todos to make the test
    5. // pass.
    6. //
    7. // Execute `rustlings hint tests6` or use the `hint` watch subcommand for a
    8. // hint.
    9. // I AM NOT DONE
    10. struct Foo {
    11. a: u128,
    12. b: Option<String>,
    13. }
    14. /// # Safety
    15. ///
    16. /// The `ptr` must contain an owned box of `Foo`.
    17. unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box {
    18. // SAFETY: The `ptr` contains an owned box of `Foo` by contract. We
    19. // simply reconstruct the box from that pointer.
    20. let mut ret: Box = unsafe { Box::from_raw(ptr) };
    21. // todo!("The rest of the code goes here")
    22. ret.b = Some("hello".to_owned());
    23. ret
    24. }
    25. #[cfg(test)]
    26. mod tests {
    27. use super::*;
    28. use std::time::Instant;
    29. #[test]
    30. fn test_success() {
    31. let data = Box::new(Foo { a: 1, b: None });
    32. let ptr_1 = &data.a as *const u128 as usize;
    33. // SAFETY: We pass an owned box of `Foo`.
    34. let ret = unsafe { raw_pointer_to_box(Box::into_raw(data)) };
    35. let ptr_2 = &ret.a as *const u128 as usize;
    36. assert!(ptr_1 == ptr_2);
    37. assert!(ret.b == Some("hello".to_owned()));
    38. }
    39. }

    92.配置本地环境

    首先我们得知道配置文件放在build.rs里面!!!

    首先参考文档之后写上指令

    "rustc-env=TEST_FOO={}",

    然后注释掉下面的 your_command变量即可通过test7

    93. 函数签名设置编译参数

    这个标签指定了编译的配置参数key-value

    设置编译的key-value即可,详情参照文档

    let your_command = "rustc-cfg=feature = \"pass\"";

     

     94.设置#[link_name = "myName"]和#[no_mangle]获取原汁原味特定的函数名

    有时候经过名称修饰或者编译,函数名会发生变化,在同语言下没有影响,但是在不同语言交互的时候可能会找不到指定的函数名,所以将函数名称固定可以避免此种情况发生

    1. // tests9.rs
    2. //
    3. // Rust is highly capable of sharing FFI interfaces with C/C++ and other statically compiled
    4. // languages, and it can even link within the code itself! It makes it through the extern
    5. // block, just like the code below.
    6. //
    7. // The short string after the `extern` keyword indicates which ABI the externally imported
    8. // function would follow. In this exercise, "Rust" is used, while other variants exists like
    9. // "C" for standard C ABI, "stdcall" for the Windows ABI.
    10. //
    11. // The externally imported functions are declared in the extern blocks, with a semicolon to
    12. // mark the end of signature instead of curly braces. Some attributes can be applied to those
    13. // function declarations to modify the linking behavior, such as #[link_name = ".."] to
    14. // modify the actual symbol names.
    15. //
    16. // If you want to export your symbol to the linking environment, the `extern` keyword can
    17. // also be marked before a function definition with the same ABI string note. The default ABI
    18. // for Rust functions is literally "Rust", so if you want to link against pure Rust functions,
    19. // the whole extern term can be omitted.
    20. //
    21. // Rust mangles symbols by default, just like C++ does. To suppress this behavior and make
    22. // those functions addressable by name, the attribute #[no_mangle] can be applied.
    23. //
    24. // In this exercise, your task is to make the testcase able to call the `my_demo_function` in
    25. // module Foo. the `my_demo_function_alias` is an alias for `my_demo_function`, so the two
    26. // line of code in the testcase should call the same function.
    27. //
    28. // You should NOT modify any existing code except for adding two lines of attributes.
    29. // I AM NOT DONE
    30. extern "Rust" {
    31. fn my_demo_function(a: u32) -> u32;
    32. #[link_name = "my_demo_function"]
    33. fn my_demo_function_alias(a: u32) -> u32;
    34. }
    35. mod Foo {
    36. #[no_mangle]
    37. // No `extern` equals `extern "Rust"`.
    38. fn my_demo_function(a: u32) -> u32 {
    39. a
    40. }
    41. }
    42. #[cfg(test)]
    43. mod tests {
    44. use super::*;
    45. #[test]
    46. fn test_success() {
    47. // The externally imported functions are UNSAFE by default
    48. // because of untrusted source of other languages. You may
    49. // wrap them in safe Rust APIs to ease the burden of callers.
    50. //
    51. // SAFETY: We know those functions are aliases of a safe
    52. // Rust function.
    53. unsafe {
    54. my_demo_function(123);
    55. my_demo_function_alias(456);
    56. }
    57. }
    58. }

    ok完结

  • 相关阅读:
    数据结构 图
    vue小案列(hello world)
    【golang】pprof性能调优工具的具体使用(带案例)
    一文帮助快速入门Django
    【技能树笔记】网络篇——练习题解析(三)
    函数8:高阶函数
    交通物流模型 | MDRGCN:用于多模式交通客流预测的深度学习模型
    Django小白开发指南
    靠近用户侧和数据,算网融合实现极致协同
    【无标题】
  • 原文地址:https://blog.csdn.net/m0_72678953/article/details/133969816