diff --git a/sdk/template/azure_template/src/function_example.rs b/sdk/template/azure_template/src/function_example.rs index e9884e610d..3ef59bcf8d 100644 --- a/sdk/template/azure_template/src/function_example.rs +++ b/sdk/template/azure_template/src/function_example.rs @@ -35,6 +35,40 @@ where println!("{:?}", list); } +/// A const function that can be evaluated at compile time +pub const fn add(a: i32, b: i32) -> i32 { + a + b +} + +/// An unsafe function that requires the caller to ensure safety +/// # Safety +/// +/// The caller must ensure that the pointer is valid and not null. +pub unsafe fn dereference_raw_pointer(ptr: *const i32) -> i32 { + *ptr // Dereferencing a raw pointer is unsafe in Rust +} + +/// An async function that can be awaited +pub async fn fetch_data(url: &str) -> Result { + // This is a simplified example - in a real case you'd use an HTTP client + // like reqwest to actually fetch data + Ok(format!("Data fetched from {}", url)) +} + +/// A function with a specific ABI (Application Binary Interface) +/// This example uses the "C" ABI for FFI (Foreign Function Interface) +pub extern "C" fn callable_from_c(value: i32) -> i32 { + value * 2 +} + +/// A function that combines several attributes: unsafe, extern, and const +/// # Safety +pub const unsafe extern "C" fn complex_function(ptr: *const u8, len: usize) -> usize { + // This is just an example - in real code, you'd have proper safety checks + let slice = std::slice::from_raw_parts(ptr, len); + slice.len() +} + /// A function returning a Result type for error handling pub fn parse_number(s: &str) -> Result { s.parse::() diff --git a/sdk/template/azure_template/src/trait_example.rs b/sdk/template/azure_template/src/trait_example.rs index 242c9c251d..94f897f4a4 100644 --- a/sdk/template/azure_template/src/trait_example.rs +++ b/sdk/template/azure_template/src/trait_example.rs @@ -44,56 +44,77 @@ impl Shape for Rectangle { /// A trait demonstrating associated types pub trait Container { /// The type of items this container holds - type Item; + type Item + where + T: Debug; /// Add an item to the container - fn add(&mut self, item: Self::Item); - - /// Get the number of items in the container - fn len(&self) -> usize; - - /// Check if the container is empty - fn is_empty(&self) -> bool { - self.len() == 0 - } + fn add(&mut self, item: Self::Item); } -/// A simple vector-based collection -#[derive(Debug)] -pub struct VecContainer { - pub items: Vec, +/// Example Box container implementing the Container trait +pub struct BoxContainer { + items: Vec>, } -/// Implementation of Container for VecContainer -impl Container for VecContainer { - type Item = T; +/// Implementation of Container for BoxContainer +impl Container for BoxContainer { + // Using "=" syntax for associated type with generics + type Item = Box where T: Debug; - fn add(&mut self, item: Self::Item) { + fn add(&mut self, item: Self::Item) { self.items.push(item); } +} - fn len(&self) -> usize { - self.items.len() - } +/// A trait that extends multiple other traits +pub trait SuperTrait: Debug + Display + Clone { + /// A method specific to SuperTrait + fn super_method(&self) -> String; } -/// A trait demonstrating associated constants -pub trait Bounded { - /// The maximum value for this type - const MAX: Self; - /// The minimum value for this type - const MIN: Self; +/// An unsafe trait example - implementors must uphold safety guarantees +/// that the Rust compiler cannot verify +/// +/// # Safety +pub unsafe trait UnsafeAccess { + /// Get a raw pointer to the internal data + /// + /// # Safety + unsafe fn get_raw_ptr(&self) -> *const u8; + + /// Get a mutable raw pointer to the internal data + /// + /// # Safety + unsafe fn get_raw_mut_ptr(&mut self) -> *mut u8; } -impl Bounded for i32 { - const MAX: i32 = i32::MAX; - const MIN: i32 = i32::MIN; +/// A struct that implements the UnsafeAccess trait +#[derive(Debug)] +pub struct RawBuffer { + data: Vec, } -/// A trait that extends multiple other traits -pub trait SuperTrait: Debug + Display + Clone { - /// A method specific to SuperTrait - fn super_method(&self) -> String; +impl RawBuffer { + /// Create a new buffer with the given size + pub fn new(size: usize) -> Self { + RawBuffer { + data: vec![0; size], + } + } +} + +/// Implementation of UnsafeAccess for RawBuffer +/// +/// This is unsafe because we're exposing raw pointers that could be misused +unsafe impl UnsafeAccess for RawBuffer { + unsafe fn get_raw_ptr(&self) -> *const u8 { + self.data.as_ptr() + } + + unsafe fn get_raw_mut_ptr(&mut self) -> *mut u8 { + self.data.as_mut_ptr() + } } /// A trait with lifetime parameters @@ -105,7 +126,10 @@ pub trait Parser<'a, T> { /// A struct implementing the Parser trait pub struct NumberParser; -impl<'a> Parser<'a, i32> for NumberParser { +impl<'a> Parser<'a, i32> for NumberParser +where + i32: 'a, +{ fn parse(&self, input: &'a str) -> Result { input.parse::().map_err(|_| "Failed to parse number") }