Skip to content

[template] Expanding template for more apiview coverage #2337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions sdk/template/azure_template/src/function_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
// 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<i32, String> {
s.parse::<i32>()
Expand Down
94 changes: 59 additions & 35 deletions sdk/template/azure_template/src/trait_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
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<T: Debug + 'static>(&mut self, item: Self::Item<T>);
}

/// A simple vector-based collection
#[derive(Debug)]
pub struct VecContainer<T> {
pub items: Vec<T>,
/// Example Box container implementing the Container trait
pub struct BoxContainer {
items: Vec<Box<dyn std::any::Any>>,
}

/// Implementation of Container for VecContainer
impl<T> Container for VecContainer<T> {
type Item = T;
/// Implementation of Container for BoxContainer
impl Container for BoxContainer {
// Using "=" syntax for associated type with generics
type Item<T: Debug> = Box<T> where T: Debug;

fn add(&mut self, item: Self::Item) {
fn add<T: Debug + 'static>(&mut self, item: Self::Item<T>) {
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<u8>,
}

/// 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
Expand All @@ -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<i32, &'static str> {
input.parse::<i32>().map_err(|_| "Failed to parse number")
}
Expand Down
Loading